System.InvalidOperationException:没有与此对象关联的进程

时间:2019-06-12 20:28:22

标签: c#

我正在调用带有代码的jar文件

Process process = new Process ();
process.StartInfo.FileName = "java";
process.StartInfo.Arguments = "-jar test.jar" + Filename;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.Start ();
process.WaitForExit ();

但仅在具有Windows 7的某些Windows上,可以正确调用该过程,但会出现异常

System.InvalidOperationException: No process associated with the object.
in System.Diagnostics.Process.EnsureState (State state)
in System.Diagnostics.Process.EnsureState (State state)
in System.Diagnostics.Process.GetProcessHandle (Int32 access, Boolean throwIfExited)
in System.Diagnostics.Process.WaitForExit (Int32 milliseconds)
in System.Diagnostics.Process.WaitForExit ()

在Windows 10上有效

也许是Windows上的补丁程序问题?

谢谢

1 个答案:

答案 0 :(得分:1)

尝试一下:

Process process = new Process ();
process.StartInfo.FileName = "java";
process.StartInfo.Arguments = "-jar test.jar " + Filename; //add space after .jar
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.Start (); //process instead of Process
process.WaitForExit ();