I often solve this by checking for other processes with the same name. The advantage/disadvantage with this is that you (or the user) can "step aside" from the check by renaming the exe. If you do not want that you could probably use the Process-object that is returned.
string procName = Process.GetCurrentProcess().ProcessName;
if (Process.GetProcessesByName(procName).Length == 1)
{
...code here...
}
It depends on your need, I think it's handy to bypass the check witout recompiling (it's a server process, which sometimes is run as a service).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…