How to print a text file using C#

16,531

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 );
}
Share:
16,531
somethingSomething
Author by

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, 2022

Comments

  • somethingSomething
    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
      Amicable over 10 years
      Have you written any code? What have you tried? Any errors?
    • Micah Armantrout
      Micah Armantrout over 10 years
      Have you done a google search ?
    • Conrad Frix
      Conrad Frix over 10 years
      You'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
      Micah Armantrout over 10 years
      The link is for winform not a console app
    • germi
      germi over 10 years
      You can use System.Windows.WinForms in a console app...
    • somethingSomething
      somethingSomething over 10 years
      I 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.