wpf 验证应用程序是否正在运行,如果没有则启动它

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12060290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 05:03:54  来源:igfitidea点击:

Verify if an app is running and start it if not

c#.netwpfprocess

提问by Bruno Heringer

I need to open an application if it is not already running.

如果应用程序尚未运行,我需要打开它。

Ex: I check if the application is running, if not I should run it for it to stay running.

例如:我检查应用程序是否正在运行,如果没有,我应该运行它以保持运行。

I've tried:

我试过了:

System.Diagnostics.Process.Start ("location of the executable");

and it will work, however, do not have the specific path of the application to open.

并且它会工作,但是,没有应用程序的特定路径打开

回答by Joseph Ferris

Based on your comment, detecting it is pretty straightforward. Just enumerate Process.GetProcesses() or look for it explicitly by Process.GetProcessByName(). There are various examples on the MSDN GetProcesses()documentation page.

根据您的评论,检测它非常简单。只需枚举 Process.GetProcesses() 或通过 Process.GetProcessByName() 显式查找它。MSDN GetProcesses()文档页面上有各种示例。

Launching an arbitrary application, though, is not as simple. If it is in the environmental PATH variable, you can launch it without knowing the install location - Internet Explorer, for example, which you can run by just typing IExplore.exe in your Start->Run dialog on your machine.

但是,启动任意应用程序并不那么简单。如果它在环境 PATH 变量中,您可以在不知道安装位置的情况下启动它 - 例如 Internet Explorer,您只需在计算机上的开始->运行对话框中键入 IExplore.exe 即可运行。

If you are sure that the executable is going to be in the PATH, and by you implying in your post that you can already launch it via Process.Start(), that may suffice. You can just simply then put a conditional gate in to see if it is present in the running processes before invoking Process.Start() via a call to GetProcessByName - so something like:

如果您确定可执行文件将在 PATH 中,并且您在帖子中暗示您已经可以通过 Process.Start() 启动它,那可能就足够了。在通过调用 GetProcessByName 调用 Process.Start() 之前,您可以简单地放入一个条件门,以查看它是否存在于正在运行的进程中 - 例如:

var runningProcessByName = Process.GetProcessesByName("iexplore");
if (runningProcessByName.Length == 0)
{
    Process.Start("iexplore.exe");
}

You obviously would use the name of the application you are looking to check for / execute in place of "iexplore". Note that you are looking for the executable name without extension when you search for processes, yet are including it when you attempt to launch it.

您显然会使用您要检查/执行的应用程序的名称来代替“iexplore”。请注意,您在搜索进程时正在寻找不带扩展名的可执行文件名称,但在尝试启动它时将其包括在内。

Update

更新

Here is a good example that can be easily modified for finding an arbitrary file or list of files in C#. Please bear in mind, if you are able to target any part of the path (for example, searching inside of Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), if you can be sure it is in a Program Files variant), the search will be considerably quicker. You may also want to consider storing the search result locally once the file is found, etc.:

这是一个很好的示例,可以轻松修改以在 C# 中查找任意文件或文件列表。请记住,如果您能够定位路径的任何部分(例如,在Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)内部搜索,如果您可以确定它在 Program Files 变体中),则搜索会快很多。您可能还需要考虑在找到文件后将搜索结果存储在本地等:

Quickest way in C# to find a file in a directory with over 20,000 files

C# 中在包含 20,000 多个文件的目录中查找文件的最快方法

回答by czubehead

Try mutex:

尝试互斥锁:

bool Creatednew;
Mutex mut=new Mutex(true,"someuniqeid",out Creatednew);
if(Creatednew)//this app isn't already running
{
//run your app
}
else
Debug.WriteLine("new app was terminated");