3

Why is this

ProcessStartInfo myProcess = new ProcessStartInfo(path);
myProcess.UserName = username;
myProcess.Password = MakeSecureString(password);
myProcess.UseShellExecute = false;
Process.Start(myProcess);

working, but

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);

is not.

I wanted to use the second one because of this question: https://stackoverflow.com/a/2621943/1306186

I am constantly getting a file not found exception... Any ideas?

Edit:
Path is for example @"C:\Users\User\Desktop\ConsoleApplication2.exe"

Community
  • 1
  • 1
Zulakis
  • 7,859
  • 10
  • 42
  • 67
  • 2
    Did you print the result of `Path.GetFileName(path);` and `Path.GetDirectoryName(path);`? Are they correct? – Tudor Sep 16 '12 at 20:10
  • put a breakpoint in your code and compare the values in the `ProcessStartInfo` instance and the second one. See what differs. – Tejas Sharma Sep 16 '12 at 20:11
  • if you change `myProcess.UseShellExecute = true;` the both are working . – Hamed Sep 16 '12 at 20:17
  • @Tudor: Yes I did. `Path.GetDirectoryName(path)->C:\Users\User\Desktop` and `Path.GetFileName(path)->ConsoleApplication2.exe` – Zulakis Sep 17 '12 at 14:38

2 Answers2

5

This bit is wrong

myProcess.FileName = Path.GetFileName(path);

this should be

myProcess.FileName = path;

Pass in C:\SomeDir\SomeApp.exe and the code you have will set the filename to SomeApp.exe, which it can't find. Count yourself lucky, there are circumstances where it could have (e.g. your app and the app you want to run being in the same folder), and then you would have possibly got a funny when deploying.

Community
  • 1
  • 1
Tony Hopkinson
  • 20,172
  • 3
  • 31
  • 39
  • This is exactly the same as my first example which I already marked as 'working'... The constructur is doing nothing but setting the `FileName` attribute. – Zulakis Sep 17 '12 at 14:45
  • @Zulakis, and your point is? 2nd example didn't work because it couldn't locate ConsoleApplication2.exe. First example, (and the suggested corrections does) did because you told it ConsoleApplication2.exe is in the C:\Users\User\Desktop where it can find it. If this wasn't the answer you wanted change your question to give us a clue as to the one you did... – Tony Hopkinson Sep 17 '12 at 16:53
  • According to the linked post in my entry post doing so is wrong... `[QUOTE]I had my entire path and file name set in the filename attribute, instead, place your path under the 'Working Directory' property, leave the filename property just for the 'file name'. [/QUOTE]` .... – Zulakis Sep 17 '12 at 17:50
  • That's the working directory of the application you are trying to srart – Tony Hopkinson Sep 17 '12 at 22:24
  • That's a different issue. Your code sets ConsoleApplication2's working directory. If you wanted to use it to solve your problem, you'd need to set the working directory of the calling application to where ConsoleApplication2 is, and I definitely do not recommend doing so. – Tony Hopkinson Sep 17 '12 at 22:35
0

I would try using Path.GetFullPath() instead of simply Path.GetFileName() since the constructor initializes the FileName with the full path when you use it with the string parameter.

Tejas Sharma
  • 3,420
  • 22
  • 35