如何使用C#打印文本文件

如何使用C#打印文本文件

问题描述:

如何在C#中打印文本文件?在控制台应用程序中。

How can I print a text file in C#? In a console application.

这是我发现的: msdn示例和此

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.

这是我发现的:

    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);

因为你说这个问题是主题不相关的,这里是一个链接到我想了解:这是.Net框架的文档,这就是为什么我要求

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.

您可以使用PRINT动词使用进程 ProcessStartInfo 类:

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);