如何通过一个过程调用将多个文件发送到打印机 [英] How to send multiple files to the printer with one Process call

查看:96
本文介绍了如何通过一个过程调用将多个文件发送到打印机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从硬盘驱动器打印多个PDF文件.我发现了这个美丽的如何将文件发送到打印机的解决方案.这种解决方案的问题在于,如果要打印多个文件,则必须等待每个文件才能完成该过程.

I need to print multiple PDF-files from the hard-drive. I have found this beautiful solution of how to send a file to the printer. The problem with this solution is that if you want to print multiple files you have to wait for each file for the process to finish.

在命令外壳中可以使用具有多个文件名的同一命令:print /D:printerName file1.pdf file2.pdf 一个电话就可以全部打印出来.

in the command shell it is possible to use the same command with multiple filenames: print /D:printerName file1.pdf file2.pdf and one call would print them all.

不幸的是,仅仅将所有文件名放入ProcessStartInfo中是行不通的

unfortunately simply just to put all the filenames into the ProcessStartInfo doesn't work

string filenames = @"file1.pdf file2.pdf file3.pdf"
ProcessStartInfo info = new ProcessStartInfo();
info.Verb = "print";
info.FileName = filenames;

也不会将文件名作为Process

info.Arguments = filename;

我总是收到错误:找不到文件!

I always get the error: Cannot find the file!

如何通过一个流程调用打印大量文件?

How can I print a multitude of files with one process call?

这是我现在如何使用它的一个示例:

Here is an example of how I use it now:

public void printWithPrinter(string filename, string printerName)
{

    var procInfo = new ProcessStartInfo();    
    // the file name is a string of multiple filenames separated by space
    procInfo.FileName = filename;
    procInfo.Verb = "printto";
    procInfo.WindowStyle = ProcessWindowStyle.Hidden;
    procInfo.CreateNoWindow = true;

    // select the printer
    procInfo.Arguments = "\"" + printerName + "\""; 
    // doesn't work
    //procInfo.Arguments = "\"" + printerName + "\"" + " " + filename; 

    Process p = new Process();
    p.StartInfo = procInfo;

    p.Start();

    p.WaitForInputIdle();
    //Thread.Sleep(3000;)
    if (!p.CloseMainWindow()) p.Kill();
}

推荐答案

下面的方法应该起作用:

Following should work:

public void PrintFiles(string printerName, params string[] fileNames)
{
    var files = String.Join(" ", fileNames);
    var command = String.Format("/C print /D:{0} {1}", printerName, files);
    var process = new Process();
    var startInfo = new ProcessStartInfo
    {
        WindowStyle = ProcessWindowStyle.Hidden,
        FileName = "cmd.exe",
        Arguments = command
    };

    process.StartInfo = startInfo;
    process.Start();
}

//CALL
PrintFiles("YourPrinterName", "file1.pdf", "file2.pdf", "file3.pdf");

这篇关于如何通过一个过程调用将多个文件发送到打印机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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