检查进程是否在c#中为null? [英] check process if null in c# ?
本文介绍了检查进程是否在c#中为null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好所有
请我尝试使用此代码在c#windows应用程序中运行进程
hello all
please i try to use this code to run process in c# windows application
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);
但是如果Process P找不到我需要它的程序走另一条道路..
问题是我无法检查p是否为空,如果找不到正确的路径代码停止运行
?
help please:)
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 :)
推荐答案
如果没有进程已经启动,无论出于何种原因,这个调用确实返回null(你还能期待什么?:-):< a href =http://msdn.microsoft.com/en-us/library/vstudio/h6ak8zt5> http://msdn.microsoft.com/en-us/library/vstudio/h6ak8zt5 [< a href =http://msdn.microsoft.com/en-us/library/vstudio/h6ak8zt5target =_ blanktitle =New Window> ^ ]。
那么,有什么问题,检查if(p == null)...
?什么都没有停止运行。
(这个帖子的答案,不是问题;一切都很清楚。)
-SA
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
也许你可以先检查是否存在可执行文件,然后继续处理。
见这里 File.Exists [ ^ ]
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);
// ...
}
这篇关于检查进程是否在c#中为null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文