Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

Run Only One Copy Of Application

, 29 Mar 2011
Rate this:
Please Sign up or sign in to vote.
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).
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Neil A. Harding

United States United States
No Biography provided

Comments and Discussions

 
GeneralReason for my vote of 1 reason for my vote of 1. I don't see... Pinmembercharles henington13-Apr-11 3:25 
GeneralRe: The code is in Main before you do App app = new App(); app.R... PinmemberNeil A. Harding13-Apr-11 6:03 
GeneralWould you mind telling us what "Remote" refers to in your co... Pinmemberloyal ginger29-Mar-11 3:48 
GeneralBe aware that this Code won't work in Terminal Server Sessio... PinmemberNenad L.22-Mar-11 3:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 29 Mar 2011
Article Copyright 2011 by Neil A. Harding
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid