我想替换cMyProcessName(在我的例子中)程序化,我不想使用刺常量!是否有可能获得自己的进程名称?
这是代码:
private const string cMyProcessName = "MyProcessName";
if (GetProcessCount(cMyProcessName) > 1)
{
System.Threading.Thread.Sleep(2000); //Give it some time (if just restarted)
//**************************************************************//
if (GetProcessCount(cMyProcessName) > 1)
{
MessageBox.Show("MyProcessName is already running. Exiting.", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
//**************************************************************//
}
public static int GetProcessCount(string processName)
{
Process[] ps = Process.GetProcessesByName(processName);
return ps.Length;
}
可能重复(http://stackoverflow.com/问题/ 3545240/prevent-ac-sharp-application-from-running-more-one-instance) –
(事实并非如此,我只想要进程名称) – Niklas