我想用程序化替换c Process Name(在我的例子中),我不想使用sting常量!
这是代码:
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;
}
答案 0 :(得分:6)
试试这个:
Process p = Process.GetCurrentProcess();
string cMyProcessName = p.ProcessName;
答案 1 :(得分:1)
Process.GetCurrentProcess Method就是您所需要的:
string processName = Process.GetCurrentProcess().ProcessName;
答案 2 :(得分:1)
您可以使用以下方法简化代码:
Process currentProcess = Process.GetCurrentProcess();