无论如何,在生成进程时是否指定了PrintTo打印机?

Imm*_*ith 10 c# process shellexecute printers

是)我有的

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

该文件可以是以下格式:Excel,Excel报表,Word或PDF.

我目前正在做的是用文件的路径生成一个进程然后启动进程; 但是我也正在尝试修复我添加的错误功能,它根据指定的设置将动词"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);
}
Run Code Online (Sandbox Code Playgroud)

我是怎么做的......

仍然难倒......可能会像微软那样称呼它,'那是设计'.

dat*_*ore 22

以下适用于我(使用*.doc和*.docx文件测试)

使用"System.Windows.Forms.PrintDialog"显示windows printto对话框,对于"System.Diagnostics.ProcessStartInfo",我只选择所选的打印机:)

只需使用Office文件的FullName(路径+名称)替换FILENAME即可.我认为这也适用于其他文件......

// 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 对我来说也适用于 pdf 文件!谢谢 - 帮了我很多 (2认同)

Row*_*haw 5

理论上,根据MSDN 上的一篇文章,您应该能够将其更改为(未经测试):

// Determine wether to just open or print
if (Print)
{
    if (PrinterName != null)
    {
        pStartInfo.Arguments = "\"" + PrinterName + "\"";
    }

    pStartInfo.CreateNoWindow = true;
    pStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    pStartInfo.UseShellExecute = true;
    pStartInfo.WorkingDirectory = sDocPath;

    pStartInfo.Verb = "PrintTo";
}
Run Code Online (Sandbox Code Playgroud)