Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Run Only One Copy Of Application

0.00/5 (No votes)
29 Mar 2011 1  
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 =...
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).
C#
string processName = Process.GetCurrentProcess().ProcessName;
Process[] instances = Process.GetProcessesByName(processName);

if (instances.Length > 1)
{
    //Try and send a message with args[]
    StringBuilder text = new StringBuilder();
    foreach (string str in args)
    {
        if (File.Exists(str))   //If it is a file, I want full path
        {
            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());
    // MessageBox.Show("This application is already running");
    return;
}


The Remote class is available here. Simple Interprocess Communication[^]

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here