c# - 无论如何在生成进程时指定 PrintTo 打印机?

标签 c# process shellexecute printers

我有什么

我目前正在编写一个程序,它接受一个指定的文件并对其执行一些操作。目前它打开它,和/或将它附加到电子邮件并将其邮寄到指定地址。

文件可以是以下格式:Excel、Excel Report、Word 或 PDF。

我目前正在做的是使用文件路径生成一个进程,然后启动该进程;然而,我也在尝试修复我添加的 bug 功能,该功能将动词“PrintTo”添加到启动信息中,具体取决于指定的设置。

我需要什么

我要完成的任务是我想打开文档,然后将其自身打印到程序本身指定的指定打印机。之后,文件应该会自动关闭。

如果没有办法通用地执行此操作,我们也许可以想出一种方法来为每种单独的文件类型执行此操作。

你需要什么

这是我使用的代码:

ProcessStartInfo pStartInfo = new ProcessStartInfo();
pStartInfo.FileName = FilePath;

// Determine wether to just open or print
if (Print)
{
    if (PrinterName != null)
    {
       // TODO: Add default printer.
    }

    pStartInfo.Verb = "PrintTo";
}

// Open the report file unless only set to be emailed.
if ((!Email && !Print) || Print)
{
    Process p = Process.Start(pStartInfo);
}

我最近怎么样...

仍然感到困惑...可能会像 Microsoft 那样调用它,“这是设计使然”。

最佳答案

以下对我有效(使用 *.doc 和 *.docx 文件测试)

Windows printto 对话框通过使用“System.Windows.Forms.PrintDialog”出现,对于“System.Diagnostics.ProcessStartInfo”,我只使用选定的打印机:)

只需将 FILENAME 替换为您的 Office 文件的全名(路径+名称)。我认为这也适用于其他文件...

// Send it to the selected printer
using (PrintDialog printDialog1 = new PrintDialog())
{
    if (printDialog1.ShowDialog() == DialogResult.OK)
    {
        System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo(**FILENAME**);
        info.Arguments = "\"" + printDialog1.PrinterSettings.PrinterName + "\"";
        info.CreateNoWindow = true;
        info.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        info.UseShellExecute = true;
        info.Verb = "PrintTo";
        System.Diagnostics.Process.Start(info);
    }
}

关于c# - 无论如何在生成进程时指定 PrintTo 打印机?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3197830/

相关文章:

c# - 单独项目中的 EF5 模型看不到 DbContext 方法

c# - 使用对象作为方法参数将 C# 移植到 C++

android - 停止远程进程中的服务

bash - 在 bash 中使用 trap 终止函数

Python 进程完成

c++ - 使用 ShellExecute 打开 URL - C++ 中的 SW_SHOW MAXIMIZED 非事件窗口

c# - Windows 10 UWP 应用程序在启动画面后立即关闭/崩溃

c# - 如何获取登录后默认页面的userInformation?

带参数的 PHP shell_exec 不工作

shell - 读取shell命令输出的最佳方式