c#ProcessStartInfo

Zul*_*kis 3 c# process

为什么是这样

ProcessStartInfo myProcess = new ProcessStartInfo(path);
myProcess.UserName = username;
myProcess.Password = MakeSecureString(password);
myProcess.UseShellExecute = false;
Process.Start(myProcess);
Run Code Online (Sandbox Code Playgroud)

工作,但是

ProcessStartInfo myProcess = new ProcessStartInfo();
myProcess.FileName = Path.GetFileName(path);
myProcess.WorkingDirectory = Path.GetDirectoryName(path);
myProcess.UserName = username;
myProcess.Password = MakeSecureString(password);
myProcess.UseShellExecute = false;
Process.Start(myProcess);
Run Code Online (Sandbox Code Playgroud)

不是.

我想使用第二个因为这个问题:https://stackoverflow.com/a/2621943/1306186

我不断得到一个文件未找到异常......有什么想法吗?

编辑:
路径是例如@"C:\Users\User\Desktop\ConsoleApplication2.exe"

Ton*_*son 5

这个位错了

myProcess.FileName = Path.GetFileName(path);
Run Code Online (Sandbox Code Playgroud)

这应该是

myProcess.FileName = path;
Run Code Online (Sandbox Code Playgroud)

传入,C:\SomeDir\SomeApp.exe你拥有的代码将文件名设置为SomeApp.exe,它找不到.算一下自己很幸运,有些情况下(例如你的应用程序和你要运行的应用程序位于同一个文件夹中),然后在部署时你可能会觉得好笑.