【问题标题】:How to print a text file using C# [closed]如何使用 C# 打印文本文件 [关闭]
【发布时间】:2020-09-15 14:44:46
【问题描述】:

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

这是我发现的:msdn sample 和这个 stackoverflow:answer is the msdn sample

链接中的代码适用于 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);

既然你说这个问题是题外话而且不相关,这里是我想要学习的链接:This is documentation of the .Net framework 这就是我问这个问题的原因,我正在尝试了解各种.Net 类的使用。

【问题讨论】:

  • 你写过代码吗?你试过什么?有什么错误吗?
  • 你用谷歌搜索了吗?
  • 您必须解释为什么这些链接不适合您。如果你不这样做,你的问题可能会被当作骗子关闭
  • 该链接适用于 winform 而不是控制台应用程序
  • 您可以在控制台应用程序中使用System.Windows.WinForms...

标签: c# printing console


【解决方案1】:

您可以使用 PRINT 动词通过 ProcessProcessStartInfo 类将文件打印到默认打印机:

System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(@"C:\temp\output.txt");
psi.Verb = "PRINT";

Process.Start(psi);

如果您想在继续之前确保文件已发送到打印机,请使用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 );
}

【讨论】:

    猜你喜欢
    • 2010-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-24
    • 1970-01-01
    相关资源
    最近更新 更多