Blog of a Filipino Developer about C#, VB.NET, ASP.NET, Java, PHP, SQL Server, MySql and Oracle RSS 2.0
 Sunday, April 02, 2006

Here's a short code that checks to see if the current application is already running.

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;
      }
   }
}

Nothing really special... just my thoughts for today.

Sunday, April 02, 2006 7:53:40 AM (GMT Standard Time, UTC+00:00)  #    Comments [1] -
.NET
Sunday, April 02, 2006 7:36:36 PM (GMT Standard Time, UTC+00:00)
Hi,

No problem, the software is great, but the application has to have elevated privileges to execute that :--).
It is: Performance Monitor Users.

The reason is that .net framework uses the performance api and not the psapi to enumerate processes.

Regards
Liviu
Comments are closed.
On this page
Archive
<December 2008>
SunMonTueWedThuFriSat
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2008
Keith Rull
Sign In
Statistics
Total Posts: 260
This Year: 57
This Month: 0
This Week: 0
Comments: 116
Themes
Pick a theme:
Ads
All Content © 2008, Keith Rull
DasBlog theme 'Business' created by Christoph De Baene (delarou)