Click here to Skip to main content
12,506,280 members (54,310 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

5.3K views
2 bookmarked
Posted

Run Only One Copy Of Application

, 29 Mar 2011 CPOL
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)

Share

About the Author

Neil A. Harding
United States United States
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

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

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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 | Terms of Use | Mobile
Web02 | 2.8.160927.1 | Last Updated 29 Mar 2011
Article Copyright 2011 by Neil A. Harding
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid