我正在使用这个小代码片段来捕获具有某些参数的Java进程:
string query = "Select * From Win32_Process Where Name = 'javaw.exe'";
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
ManagementObjectCollection processList = searcher.Get();
foreach (ManagementObject obj in processList)
{
string cmdLine = obj.GetPropertyValue("CommandLine").ToString();
if (cmdLine.IndexOf("someapplication") != -1)
{
// ...
}
}
这段代码就像几天前我没有SP1 for VS2010的魅力一样。现在它在第7行抛出一个空指针异常。我正在尝试为.NET Framework 2.0编译。
帮助!? :/
答案 0 :(得分:2)
if (cmdLine != null && cmdLine.IndexOf("someapplication") != -1)
答案 1 :(得分:1)
它可能与SP1关系不大,与Java更新有关。只需检查null
:
string query = "Select * From Win32_Process Where Name = 'javaw.exe'";
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
ManagementObjectCollection processList = searcher.Get();
foreach (ManagementObject obj in processList)
{
object cmdLineValue = obj.GetPropertyValue("CommandLine");
if(cmdLineValue != null) {
string cmdLine = cmdLineValue.ToString();
if (cmdLine.IndexOf("someapplication") != -1)
{
// ...
}
}
}