C# Activate Previous Application Instance

March 9, 2010

Continuing the following post and as an answer to Jasper: http://blogs.microsoft.co.il/blogs/asafshelly/archive/2010/03/02/c-close-previous-application-instance.aspx   The following code will make sure that only one instance is running by exiting if an instance is already running, and activating that instance. static extern bool SetForegroundWindow(IntPtr hWnd);static bool ActivateApplicationAlreadyRunning(){   string proc = Process.GetCurrentProcess().ProcessName;   Process processes = Process.GetProcessesByName(proc);   if (processes.Length < 2) return (false);   foreach (Process process in processes)   {      if (process.Id != Process.GetCurrentProcess().Id)      {         SetForegroundWindow(process.MainWindowHandle);         return (true);      }   }   return false;}   static void Main(){   if (ActivateApplicationAlreadyRunning()) return; .....
tags: ,

Verify Installation of a custom device using C#

Hi all, Using my WinUSB C# component, I also needed to automatically install the driver if it is not already installed. The INF file defines a new device class by its GUID. This means that the class does not exist on the machine if the device is not installed. Here is the code: internal static extern bool SetupDiGetClassDescription(ref Guid ClassGuid,StringBuilder classDescription, Int32 ClassDescriptionSize, ref UInt32 RequiredSize);public static bool IsDeviceClassInstalled(string deviceClassGuid){   return (IsDeviceClassInstalled(new Guid(deviceClassGuid)));} public static bool IsDeviceClassInstalled(Guid deviceClassGuid){   StringBuilder deviceClassDescription = new StringBuilder(256);   UInt32 retLength = 0;   SetupDiGetClassDescription(ref deviceClassGuid, deviceClassDescription, deviceClassDescription.Capacity, ref retLength);   return (deviceClassDescription.Length >...
no comments

C# Close previous application instance

March 2, 2010

Continuing this post: http://kseesharp.blogspot.com/2008/11/c-check-if-application-is-already.html It is possible to close the currently running instance of the application:         static bool CloseApplicationAlreadyRunning()      {         string proc = Process.GetCurrentProcess().ProcessName;         Process processes = Process.GetProcessesByName(proc);         if (processes.Length < 2) return (false);         foreach (Process process in processes)         {            if (process.Id != Process.GetCurrentProcess().Id)            {               process.CloseMainWindow();               if (process.WaitForExit(6000)) return (false);               else return (true);            }         }         return false;      }   Modify the timeout appropriately.   Asaf http://asyncop.com/
one comment