无论如何,在生成进程时是否要指定PrintTo打印机? [英] Is there anyway to specify a PrintTo printer when spawning a process?

查看:85
本文介绍了无论如何,在生成进程时是否要指定PrintTo打印机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am currently writing a program which takes a specified file and the performs some action with it. Currently it opens it, and/or attaches it to an email and mails it to specified addresses.

文件可以采用以下格式:Excel,Excel报告,Word或PDF.

The file can either be of the formats: Excel, Excel Report, Word, or PDF.

我当前正在做的是生成一个带有文件路径的进程,然后启动该进程;但是我还试图修复我添加的 bug 功能,该功能根据指定的设置将动词"PrintTo"添加到启动信息中.

What I am currently doing is spawning a process with the path of the file and then starting the process; however I also am trying to fix a bug feature that I added which adds the verb 'PrintTo' to the startup information, depending on a specified setting.

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

The task I am trying to accomplish is that I would like to have the document open and then print itself to a specified printer named within the program itself. Following that up, the file should then close itself automatically.

如果没有通用的方法,我们也许可以为每种单独的文件类型提供一种方法.

If there is no way to do this generically, we might be able to come up with a way to do it for each separate file type.

这是我正在使用的代码:

Here is the code I'm using:

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

我怎么样...

仍然很困惑……可能像微软那样称呼它,那是设计使然".

How I'm doing...

Still stumped... might call it like Microsoft does,'That was by design'.

推荐答案

以下内容对我有用(已测试* .doc和* .docx文件)

The following works for me (tested with *.doc and *.docx files)

通过使用"System.Windows.Forms.PrintDialog"出现Windows printto对话框,对于"System.Diagnostics.ProcessStartInfo",我只是选择了打印机:)

the windows printto dialog appears by using the "System.Windows.Forms.PrintDialog" and for the "System.Diagnostics.ProcessStartInfo" I just take the selected printer :)

只需将 FILENAME 替换为Office文件的全名(路径+名称)即可.我认为这也可以与其他文件一起使用...

just replace the FILENAME with the FullName (Path+Name) of your Office file. I think this will also work with other files...

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

这篇关于无论如何,在生成进程时是否要指定PrintTo打印机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆