我有什么
我目前正在编写一个程序,它接受一个指定的文件并对其执行一些操作。目前它打开它,和/或将它附加到电子邮件并将其邮寄到指定地址。
文件可以是以下格式: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/