场景
在Unity中使用创建新进程的方式打印pdf遇到错误,无法正常打印。
实例代码:
/// <summary>
/// 打印pdf文件
/// </summary>
/// <param name="filePath">文件地址</param>
private void PrintPDF(string filePath)
{
Process process = new Process();
try
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = true;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.UseShellExecute = true;
startInfo.FileName = filePath;
startInfo.Verb = "print";
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
Debug.Log("打印成功!\n" );
}
catch(Exception e)
{
Debug.Log("打印失败!\n" + e);
}
finally
{
process.Close();
}
}
错误提示
System.ComponentModel.Win32Exception (0x80004005): mono-io-layer-error (1155)
at System.Diagnostics.Process.StartWithShellExecuteEx (System.Diagnostics.ProcessStartInfo startInfo) [0x00102] in <d8cd0ec6fc774382b0a4c707a194e94c>:0
at System.Diagnostics.Process.Start () [0x00032] in <d8cd0ec6fc774382b0a4c707a194e94c>:0
at (wrapper remoting-invoke-with-check) System.Diagnostics.Process.Start()
“mono-io-layer-error (1155)”意思是“没有对应进程与之关联”。
造成原因
改方法是通过执行目标文件的默认打开程序所支持的打印(startInfo.Verb = “print”;)动作来实现的,所以如果打印的目标文件默认打开程序未设置或不支持将会执行异常!