没有任何应用程序与C#中的此操作异常的指定文件关联

时间:2018-08-12 06:35:09

标签: c# winforms

我尝试使用类Process进行打印,但出现此异常:

“此应用程序没有与指定文件关联的应用程序”

这是我的代码:

byte[] fileStresm = getFileStresm();
string filePath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".pdf");
File.WriteAllBytes(filePath, fileStresm );

try
{
   Process p = new Process();
   p.StartInfo = new ProcessStartInfo()
   {
      CreateNoWindow = true,
      Verb = "print",
      FileName = filePath //put the correct path here
   };
   p.Start();
}
catch (Exception ex)
{
   MessageBox.Show(ex.Message);
}

1 个答案:

答案 0 :(得分:0)

需要添加此属性:

  

UseShellExecute = true

代码:

byte[] fileStresm = getFileStresm();
string filePath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".pdf");
File.WriteAllBytes(filePath, fileStresm );

try
{
   Process p = new Process();
   p.StartInfo = new ProcessStartInfo()
   {
     CreateNoWindow = true,
     Verb = "print",
     FileName = filePath //put the correct path here
     UseShellExecute = true
   };
   p.Start();
}
catch (Exception ex)
{
   MessageBox.Show(ex.Message);
}  
相关问题