Here's a short code that checks to see if the current application is already running.
[code language="C#"]
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
namespace KeithRull.CS.Windows.CheckIfAppIsAlreadyRunning
{
class Program
{
static void Main(string[] args)
{
if (IsAppAlreadyRunning())
{
Console.WriteLine("This application is already running... Press any key to exit.");
}
else
{
Console.WriteLine("App now running!");
// ... do whatever you want to do here.
}
Console.ReadLine();
}
public static bool IsAppAlreadyRunning()
{
bool isAlreadyRunning = false;
Process currentProcess = Process.GetCurrentProcess();
Process[] processes = Process.GetProcesses();
foreach (Process process in processes)
{
if (currentProcess.Id != process.Id)
{
if (currentProcess.ProcessName == process.ProcessName)
{
isAlreadyRunning = true;
}
}
}
return isAlreadyRunning;
}
}
}
[/code]
Nothing really special... just my thoughts for today.
Posted
04-01-2006 11:47 PM
by
keithrull