简体   繁体   中英

How to print a text file content in .Net using c#

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://docs.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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM