我想替換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