0

I want to open up an existing instance if my program is already running only if its running the same version. I noticed that this question was asked for only the the name exists, but what if the version is older, I just want to notify the user that an older version is still running, please remove older version before starting this version.

The other link is this one: Return to an already open application when a user tries to open a new instance but they don't talk about closing an instance if an older or newer version is detected.

Community
  • 1
  • 1
RetroCoder
  • 2,597
  • 10
  • 52
  • 81

1 Answers1

3

In the easiest way, make check like this:

foreach (var proc in Process.GetProcesses())
            if (proc.MainModule.FileName == Process.GetCurrentProcess().MainModule.FileName)
                //Shutdown your copy.

But more complex thing could be like this:

foreach (var proc in Process.GetProcesses())
        {
            if (proc.Id == Process.GetCurrentProcess().Id) continue;
            var currName = AssemblyName.GetAssemblyName(Process.GetCurrentProcess().MainModule.FileName);
            var procName = AssemblyName.GetAssemblyName(proc.MainModule.FileName);
            if (currName.FullName == procName.FullName && /*and other parameters*/)
                return;
        }

Good luck!

P.S.:"But be careful - "GetAssemblyName" works only for managed code assemblies, so make it in try-catch wrap."

Artem Makarov
  • 874
  • 3
  • 14
  • 25
  • Thanks for the comment. Is it possible to go more in depth on a decision tree if its a newer version, current version, and an older version. I know the windows setup can perform certain operations to detect this. thx – RetroCoder Nov 29 '11 at 19:00