How to print a text file using C#
You can use the PRINT verb to print a file to the default printer using the Process and ProcessStartInfo classes:
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(@"C:\temp\output.txt");
psi.Verb = "PRINT";
Process.Start(psi);
If you want to make sure the file has been sent to printer before continuing, use Process.WaitForExit()
. It may be required, for example, to prevent deletion of that file before it has been printed:
static void PrintText( string text )
{ string filegen, filetxt;
ProcessStartInfo psi;
Process proc;
filegen = Path.GetTempFileName();
filetxt = filegen + ".txt";
File.Move( filegen, filetxt );
File.AppendAllText( filetxt, text );
psi = new ProcessStartInfo( filetxt );
psi.Verb = "PRINT";
proc = Process.Start( psi );
proc.WaitForExit();
File.Delete( filetxt );
}
![somethingSomething](https://i.stack.imgur.com/Y3G0s.jpg?s=256&g=1)
somethingSomething
myreddIT reddiWisdom is knowing that you are nothing, Love is knowing that you are everything and between the two your life moves. First time I was introduced to Linux was in 2007, it was Kubuntu and this person told me about the philosophy and I was told that you are not a real Linux user if you are not programming in Linux. I've been using Linux for everyday use since 2009. I started to use Linux for real in 2012. L'amour, la chaleur et la paix! ´ó ^ĸØ←←{}¬^²³»»»»»»»»»»»»»»»»»»»»»»»»»»»» P.S. Earth is for f * * K sake flat as a pancake, I kid you not The Best Flat Earth Documentary ...... If you want to know more goto ericdubay.com or find Eric Dubay on youtube(https://www.youtube.com/channel/UC0_CSKUIVVFlfocgezQEBDg/videos) Humans are in jail and everthing in our society is made to imprison us, but don't despair help is here and things are changing. We were seen as threats to the real world and illegal because we are 97% ape but what they didn't know is that we are 3% g o d l y watch this and see that this is not a human song, there are beings and higher entities everywhere:Grimes - Genesis. The world we live in is not a world, it's a highly advanced computer, the creators name is 42. There are many secrets hiding in movies. If you don't believe me then,↓↓↓↓ Watch this youtube song: (Grimes - We Appreciate Power) https://www.youtube.com/watch?v=gYG_4vJ4qNA When you listen to music, notice how the singer is talking to someone, it could be you he's talking to but there is also a being you should know about that all songs are referering to:↓↓↓↓↓↓↓ Watch and listen to this song; (Gwen Stefani - What You Waiting For) stupid boy, take a chance you st*** Check this website out, it's awsome: https://debgen.simplylinux.ch/
Updated on June 04, 2022Comments
-
somethingSomething about 2 years
How can I print a text file in C#? In a console application.
This is what I've found: msdn sample and this stackoverflow:answer is the msdn sample
The code from the links are for windows forms applications, and does'nt work in an console appliction.
Here is what I've found:
string fileName = @"C:\data\stuff.txt"; ProcessStartInfo startInfo; startInfo = new ProcessStartInfo(fileName); if (File.Exists(fileName)) { int i = 0; foreach (String verb in startInfo.Verbs) { // Display the possible verbs. Console.WriteLine(" {0}. {1}", i.ToString(), verb); i++; } } startInfo.Verb = "print"; Process.Start(startInfo);
Since you say this question is off topic and not relevant here is a link to what I'm trying to learn: This is documentation of the .Net framework and this is why I'm asking the question, I'm trying to learn about the various uses of .Net classes.
-
Amicable over 10 yearsHave you written any code? What have you tried? Any errors?
-
Micah Armantrout over 10 yearsHave you done a google search ?
-
Conrad Frix over 10 yearsYou'll have to explain why those links didn't work for you. If you don't your question might get closed as a dupe
-
Micah Armantrout over 10 yearsThe link is for winform not a console app
-
germi over 10 yearsYou can use
System.Windows.WinForms
in a console app... -
somethingSomething over 10 yearsI don't know anything about the code, I'm learning C# and was looking for code to try to understand and work with, I have alot of books on C# but wanted to practice on something else, as I was a little bored with reading, and punching code. Just want some fresh samples to look at and understand, to do some specific tasks, like printing an document from my program.
-