Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在生成进程时,是否仍要指定PrintTo打印机? 我所拥有的_C#_Process_Shellexecute_Printers - Fatal编程技术网

C# 在生成进程时,是否仍要指定PrintTo打印机? 我所拥有的

C# 在生成进程时,是否仍要指定PrintTo打印机? 我所拥有的,c#,process,shellexecute,printers,C#,Process,Shellexecute,Printers,我目前正在编写一个程序,它接受一个指定的文件,并用它执行一些操作。目前,它会将其打开,和/或将其附加到电子邮件并将其发送到指定的地址 该文件可以是以下格式之一:Excel、Excel报告、Word或PDF 我目前正在做的是用文件的路径生成一个进程,然后启动该进程;不过,我也在尝试修复我添加的一个bug功能,该功能将动词“PrintTo”添加到启动信息中,具体取决于指定的设置 我需要什么 我试图完成的任务是打开文档,然后将其自身打印到程序本身中指定的打印机上。之后,文件将自动关闭 如果没有通用的方

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

该文件可以是以下格式之一:Excel、Excel报告、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);
}
我现在怎么样。。。
仍然被难住了。。。可能会像微软那样称之为“这是设计的”。

理论上,根据您的要求,您应该能够将其更改为(未经测试):


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

通过使用“System.windows.Forms.PrintDialog”显示windows printto对话框,对于“System.Diagnostics.ProcessStartInfo”,我只选择所选打印机:)

只需将文件名替换为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);
    }
}

从罗兰·肖那里得到:

               ProcessStartInfo startInfo = new ProcessStartInfo(Url)
                {
                    Verb = "PrintTo",
                    FileName = FilePath,
                    CreateNoWindow = true,
                    WindowStyle = ProcessWindowStyle.Hidden,
                    UseShellExecute = true,
                    Arguments = "\"" + PrinterName+ "\"",
                };
                Process.Start(startInfo);
文件路径类似于“D:\EECSystem\AttachedFilesUS\53976793.pdf”

PrinterName是您的打印机名称


复制代码,它会起作用。

可能也会使用pdf文件复制我的作品!谢谢-帮了我一个忙,我们可以通过打印对话框传递整个打印设置吗?@huMpty duMpty:你说的“传递整个打印设置”是什么意思?您可以使用“yourPrintDialog1.PrinterSettings”从打印对话框接收所有打印设置。这对我来说是可行的,但如果打印机不可用或未连接怎么办。在这种情况下,我们是否可以向用户显示打印机不可用的错误消息。@AvneeshsSrivastava:未测试打印机是否必须可用。也许windows printDialog会处理它…我们可以将打印对话框中的整个打印设置作为参数传递吗?@huMptyduMpty No.
ShellExecute
仅支持发送文档和打印机名称是否有方法传递打印机设置?@huMptyduMpty处理打印机设置中的详细信息的打印的唯一方法对话框,用于自行处理打印(即页面布局等)。没有将设置传递给任意客户端应用程序的标准方法。
               ProcessStartInfo startInfo = new ProcessStartInfo(Url)
                {
                    Verb = "PrintTo",
                    FileName = FilePath,
                    CreateNoWindow = true,
                    WindowStyle = ProcessWindowStyle.Hidden,
                    UseShellExecute = true,
                    Arguments = "\"" + PrinterName+ "\"",
                };
                Process.Start(startInfo);