C# 如果进程已在运行,则出现Null异常
您好,下面的代码正在运行,但是如果其中一个程序已经打开,它将为GetProcessByName行抛出一个null异常 如果进程一直在运行,如何正确跟踪进程C# 如果进程已在运行,则出现Null异常,c#,.net,C#,.net,您好,下面的代码正在运行,但是如果其中一个程序已经打开,它将为GetProcessByName行抛出一个null异常 如果进程一直在运行,如何正确跟踪进程 string path = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "SomeFolder\\Folder\\Output"); string fname = System.IO.Pat
string path = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "SomeFolder\\Folder\\Output");
string fname = System.IO.Path.Combine(path, Title); //<--- Title property will be like text.xlsx, test.docx etc)
Process sproc = Process.Start(fname);
string pname = sproc.ProcessName;
Process info = Process.GetProcessesByName(pname).FirstOrDefault();
using (Process eProcess = info)
{
eProcess.MainWindowTitle.Contains(fname);
eProcess.WaitForExit();
//Do Other Stuff
}
string path=System.IO.path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),“SomeFolder\\Folder\\Output”);
字符串fname=System.IO.Path.Combine(路径,标题)// 为什么不使用sproc
而不是info
要回答此问题,请返回进程的友好名称(即通常没有扩展名)。所以这应该行得通
string fName = Path.GetFileNameWithoutExtension(pname);
Process info = Process.GetProcessesByName(fName).FirstOrDefault();
但如前所述,如果这是您的完整代码,则无需创建info
变量,因为您不必要地查找已存储在sproc
中的同一进程,该进程更新了源代码。关于路径和fname的问题,谢谢回复。我不认为你的更新有什么不同吗?只需将我的答案中的fname
更改为另一个名称,因为您已经在更新的代码中找到了fname
。