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

C# 如何使用C打印文本文件#,c#,printing,console,C#,Printing,Console,如何用C#打印文本文件?在控制台应用程序中 这就是我发现的:还有这个 链接中的代码用于windows窗体应用程序,在控制台应用程序中不起作用 以下是我的发现: string fileName = @"C:\data\stuff.txt"; ProcessStartInfo startInfo; startInfo = new ProcessStartInfo(fileName); if (File.Exists(fileName))

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

这就是我发现的:还有这个

链接中的代码用于windows窗体应用程序,在控制台应用程序中不起作用

以下是我的发现:

    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类的各种用法。

您可以使用“打印”动词,使用和类将文件打印到默认打印机上:

如果要在继续之前确保文件已发送到打印机,请使用
Process.WaitForExit()
。例如,可能需要防止在打印文件之前删除该文件:

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

你写过代码吗?你试过什么?有错误吗?你做过谷歌搜索吗?你必须解释为什么那些链接不适合你。如果您不这样做,您的问题可能会以副本形式关闭此链接是针对winform而不是控制台应用程序的。您可以在控制台应用程序中使用
System.Windows.WinForms
。。。
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 );
}