0

I am having a text file with few lines in them. I just want to send this file to printer of my choice. Why is this so hard? MSDN and google search points me to use : https://learn.microsoft.com/en-us/dotnet/api/system.drawing.printing.printdocument.print?view=dotnet-plat-ext-6.0

But when I use the above code in my VS Code it giving me errors when building because "PagePageEventArgs" is available only in System.Drawing.Common. However, I couldnt install that in my VS Code even through NuGet Package manager.

First of all codes in above lnik looks like it is designed for a windows form and it has too much details. I get so many other errors with all those codes. I am new to programming and these is overwhelming for me and I am stuck with for a week. Can anyone please tell me how to just print a text file (all the lines in text file) using printer of my choice.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Sky scream
  • 35
  • 5
  • Are you actually trying to print from an ASP.NET webapp? – Martheen Jan 20 '22 at 04:05
  • @Martheen, I created console app to do this print. Sole purpose of this app is to do this printing as soon as file appears in directory. I am using Filewatcher to detect when a file appears in a folder and I manage to get the path of the new text file that appears. I just dont know how to print this new text file – Sky scream Jan 20 '22 at 04:17
  • 1
    Then don't tag asp.net. Using the built-in app to handle the printing is possible https://stackoverflow.com/a/19840331/529282 – Martheen Jan 20 '22 at 04:35
  • @Martheen apologize for wrong tagging. I am not sure which is which. However in the link you send me it doesnt say if we can choose a printer? If I am not working in asp.net then what is I am using? I downloaded .Net and my vs code file reads as program.cs – Sky scream Jan 20 '22 at 04:51
  • 1
    If you want a dialog, use winform https://stackoverflow.com/questions/3197830/is-there-anyway-to-specify-a-printto-printer-when-spawning-a-process or, pinvoke the default printer before calling https://stackoverflow.com/questions/971604/how-do-i-set-the-windows-default-printer-in-c – Martheen Jan 20 '22 at 05:15
  • Thank you @Martheen your guidance was very helpful and I got it working. Much appreciate your time :) – Sky scream Jan 20 '22 at 23:18

1 Answers1

1

If they are text files you could cheat and use Notepad:

        var fileToPrint = "C:\\Junk\\Junk.txt";
        var p = new System.Diagnostics.Process();
        p.StartInfo.FileName = "notepad";
        p.StartInfo.Arguments = $"/p {fileToPrint}";
        p.StartInfo.UseShellExecute = false;
        p.Start();
        p.Dispose();
        Console.WriteLine("FINISHED");

Based on code from: https://kimsereyblog.blogspot.com/2018/01/start-processes-from-c-in-dotnet-core.html

and Notepad command line options from: https://answers.microsoft.com/en-us/windows/forum/all/notepadexe-command-line-options/810760c1-a45a-4013-9544-1c1208e1b389?auth=1

SSS
  • 4,807
  • 1
  • 23
  • 44