Click here to Skip to main content
15,887,453 members
Please Sign up or sign in to vote.
2.00/5 (4 votes)
See more:
hello all
please i try to use this code to run process in c# windows application

C#
Process p = Process.Start(@"C:\Program Files\TeamViewer\Version7\TeamViewer", "-i 10.57.171.132 --Password P@ssw0rd");
            Thread.Sleep(500); // Allow the process to open it's window
            SetParent(p.MainWindowHandle, this.Handle);


but if Process P doesnt find the program i need it to go another path ..
the problem is i cant check if p is null , the code stop running if not found the correct path
?
help please :)
Posted

If no process has started, by whatever reason, this call really returns null (what else would you expect? :-): http://msdn.microsoft.com/en-us/library/vstudio/h6ak8zt5[^].

So, what's the problem, to check "if (p == null) …"? Nothing "stops running".

(This post the answer, not a question; everything is clear.)

—SA
 
Share this answer
 
v2
maybe you can first check the existence of executable, then continue with process.
see here File.Exists[^]

string fullPath = @"full_exe_path"; // fullPath may change according to your design. like a parameter for a method, or a property of a helper class, or a constant value coming from an external configuration file.
if(File.Exists(fullPath)) {
  var p = Process.Start(fullPath);
  // ...
}
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 28-Apr-14 14:26pm    
Of course, a 5, but, in addition to that, the Start can still be unsuccessful (permission problems or whatever), hence comparison to null. I answered on that.
(But the file name should never be repeated, should not be an immediate constant, but a variable/member or explicit constant (also not very useful for path names))
—S
Vedat Ozan Oner 28-Apr-14 15:16pm    
thank you. you're right about your comment. I just tried to show a solution with 'File.Exists'. But, I will update it as you indicated.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900