I actually use code like this, which allows me to detect a existing instance and to call the original instance with the command line (I expand any filenames present so that it can access the filenames even if the original path is different).
string processName = Process.GetCurrentProcess().ProcessName;
Process[] instances = Process.GetProcessesByName(processName);
if (instances.Length > 1)
{
StringBuilder text = new StringBuilder();
foreach (string str in args)
{
if (File.Exists(str))
{
text.Append(new FileInfo(str).FullName);
}
else
{
text.Append(str);
}
text.Append(' ');
}
if (text.Length > 0)
{
text.Remove(text.Length - 1, 1);
}
Remote.Send(text.ToString());
return;
}
The Remote class is available here.
Simple Interprocess Communication[
^]