Another way to make sure only one instance of your program is running.
I just saw Wes' solution, and decided to show another approach to the solution, using WMI. Here it is...
static
void Main() {
//yank off the appname.exe from the assemblies location
string[] parts = System.Reflection.Assembly.GetExecutingAssembly().Location.Split("\\".ToCharArray());
string appName = parts[parts.Length-1];
//build the wmi query
string query = "select name from CIM_Process where name = '"+appName+"'";
//load up the managementobjectsearcher with the query
System.Management.ManagementObjectSearcher searcher =
new System.Management.ManagementObjectSearcher(query);
int runcount=0;
//iterate the collection and (which should only have 1 or 2 instances, and if 3 then its already running
//1 instaces would be itself, 2 would be self and the other
foreach (System.Management.ManagementObject item in searcher.Get()) {
runcount++;
if(runcount>1) break; //only need to know if there is more then self running
}
//if we are running only self (1) then load the form, else exit
if(runcount==1) {
Application.Run(
new Form1());
}
else {
System.Windows.Forms.MessageBox.Show("Application already running, exiting.");
Application.Exit();
}
}