01.
using
System;
02.
System.Security.Principal;
03.
System.Threading;
04.
System.Windows.Forms;
05.
06.
namespace
WindowsFormsApplication2
07.
{
08.
static
class
Program
09.
10.
[STAThread]
11.
void
Main()
12.
13.
bool
ok;
14.
string
strMyAppName =
"ObscureAppName"
;
15.
strMutex = WindowsIdentity.GetCurrent().Name.ToString();
16.
strMutex = strMutex.Split(
'\\'
)[1];
17.
strMutex += strMyAppName;
18.
Mutex m =
new
Mutex(
true
, strMutex,
out
ok);
19.
if
(!ok)
20.
21.
MessageBox.Show(
"Already running"
);
22.
Application.Exit();
23.
}
24.
(ok)
25.
26.
Application.Run(
Form1());
27.
GC.KeepAlive(m);
// important!
28.
29.
30.
31.
Karl Mitschke edited Revision 7. Comment: Found the link for the original code I had modified.
Karl Mitschke edited Revision 6. Comment: Modified to have an application name, and added line numbers
Karl Mitschke edited Revision 3. Comment: Modified code to show on one line
Karl Mitschke edited Revision 4. Comment: Broke the code
Naomi N edited Revision 1. Comment: Title case, more tags
Again, thanks for your feedback.
I have modified the code to prevent such an occurrence.
Karl
Hi,
Here no other application runs,,, it is not just about the copy of the same application .. meaning if you change the strUser then other apps run with the above check in the code ,, To generalize,, a user with the above code can run only one app ,,, hence other apps having the same pattern can't run
Regards.
Thanks for your feedback.
I have verified that simply renaming the program will not allow it to run multiple copies.
Actually this method is applicable at a premitive level,,for a better approach there should be something like a registry entry check for a running copy,,, else simply renaming the program would let it run in multiples ..... and there may be lots of other ways....