在C#中识别自己的进程名称
为了防止用户运行我的应用程序的多个实例,我使用以下代码:-
Process[] pArry = Process.GetProcesses();
int nCount = 0;
foreach (Process p in pArry)
{
string ProcessName = p.ProcessName;
ProcessName = ProcessName.ToLower();
if (ProcessName.CompareTo("myApp") == 0)
{
nCount++;
}
}
if (nCount > 1)
{
MessageBox.Show(AppAlreadyRunning,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
Process.GetCurrentProcess().Kill();
}
但据我们所知,如果更改 exe 名称,进程名称也会更改。 因此,如果用户将“myApp.exe”更改为“UserApp.exe”,则此补丁将不起作用! 还有出路吗?
我在VS2010中使用C#。 谢谢!
To prevent user from running multiple instances of my application, I am using this code:-
Process[] pArry = Process.GetProcesses();
int nCount = 0;
foreach (Process p in pArry)
{
string ProcessName = p.ProcessName;
ProcessName = ProcessName.ToLower();
if (ProcessName.CompareTo("myApp") == 0)
{
nCount++;
}
}
if (nCount > 1)
{
MessageBox.Show(AppAlreadyRunning,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
Process.GetCurrentProcess().Kill();
}
But As we know, Process Name changes if change the exe name.
So If user changes "myApp.exe" to "UserApp.exe", This patch won't work!
Is there any way out?
I am using C# in VS2010.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是非常简单的过程。调用此函数会返回一个进程类,其中包含您需要的所有信息。
这是给您的一些代码。
This is very simple process. Calling this function returns a process class with all the information you need.
Here is some code for you.
这些问题看起来非常相似...
https://stackoverflow.com/questions/93989/prevent-multiple-instances-of-a-given -app-in-net
https://stackoverflow .com/questions/229565/what-is-a-good-pattern-for-using-a-global-mutex-in-c/229567
These questions seems quite similar...
https://stackoverflow.com/questions/93989/prevent-multiple-instances-of-a-given-app-in-net
https://stackoverflow.com/questions/229565/what-is-a-good-pattern-for-using-a-global-mutex-in-c/229567
我打赌您可以使用 Process.GetCurrentProcess() (就像杀死进程一样)来获取实际名称并使用它进行比较。
I bet you could use Process.GetCurrentProcess() (as you do to kill the process) to get the actual name and use that for comparison.