Click here to Skip to main content
Licence CPOL
First Posted 30 May 2009
Views 15,836
Downloads 396
Bookmarked 14 times

Compact Framework Process class that supports fully specified file paths

By | 30 May 2009 | Article
Terminate a process, or check if it is running, using the full file path. It also enumerates processes returning the full path to the running EXE file.

Introduction

I needed to be able to terminate all the processes in a specific folder.

The Process class in the .NET Compact Framework is limited in its functionality. You cannot even enumerate processes. You can find code to do this on the Internet, but all the samples I found only return the name of the EXE file of a process. What I needed was the full path to the EXE file.

After some research, I found the solution. I wrapped the result into the ProcessCE class.

What can the ProcessCE class do for you?

  • Enumerate running processes;
  • Each enumerated process contains the full path to the EXE file;
  • Check in a single function call if a process is running by specifying the full path to the EXE file;
  • Kill a process in a single function call by specifying the full path to the EXE file;
  • Find the Process ID (PID) in a single function call by specifying the full path to the EXE file.

This code is based on several code snippets and examples I found on the Internet.

Using the code

The ProcessCE class is straightforward and easy to use. Download ProcessCE.zip and add the containing ProcessCE.cs file to your project. Then, add the following using statement to the source file where you want to use the class:

using Terranova.API;

To enumerate running processes:

ProcessInfo[] list = ProcessCE.GetProcesses();
 
foreach (ProcessInfo item in list)
{
   Debug.WriteLine("Process item: " + item.FullPath);
   if (item.FullPath == @"\Windows\iexplore.exe")
       item.Kill();
}

// Sample output:
// Process item: \Windows\nk.exe
// Process item: \Windows\filesys.exe
// Process item: \Windows\device.exe
// Process item: \Windows\services.exe
// Process item: \Windows\gwes.exe
// Process item: \Windows\shell32.exe
// Process item: \Windows\poutlook.exe
// Process item: \Program Files\Geso\Discovery\TerranovaWakeUpWCE.exe

To check if a process is running:

bool result = ProcessCE.IsRunning(@"\Windows\iexplore.exe");

To find a Process ID (PID):

IntPtr pid = ProcessCE.FindProcessPID(@"\Windows\iexplore.exe");
 
if (pid == IntPtr.Zero)
  throw new Exception("Process not found.");

To terminate a process:

bool result = ProcessCE.FindAndKill(@"\Windows\iexplore.exe"); 

ProcessCE.FindAndKill() will return false if the EXE file was not found. If terminating the process fails, it will throw a Win32Exception.

Points of interest

This code is for use with Windows CE and Windows Mobile only. The code was tested on Windows Mobile 6.1 (Windows CE 5.2). Windows CE 4 should be no problem.

Tip: A code example to enumerate processes that can be found on several websites was crashing randomly with Win32 error 8 on several devices. This is an out of memory message. The solution is to add the TH32CS_SNAPNOHEAPS flag to the CreateToolhelp32Snapshot() call:

CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS | TH32CS_SNAPNOHEAPS, 0)

History

  • 30 May 2009 - Article submitted.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Frank T. van de Ven

Software Developer (Senior)

Belgium Belgium

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionMi respeto hermano!!!! Pinmembergabriel loria5:23 29 Jan '12  
QuestionSystem.OverflowException was unhandled Pinmemberninefofo9:12 16 Aug '11  
GeneralBrilliant Pinmembergaryevans2:25 4 Jul '11  
GeneralPerfect job! PinmemberRalphMaas23:57 27 Jan '10  
GeneralTrying to use this namespace in VB.net project PinmemberSvenCPA5:54 27 Jan '10  
GeneralBrilliant! PinmemberMuffadal23:56 4 Jan '10  
GeneralRe: Brilliant! PinmemberFrank T. van de Ven2:04 5 Jan '10  
GeneralThanks. finish my problem in 10 minutes PinmemberKeshmahajan11:36 10 Dec '09  
GeneralRe: Thanks. finish my problem in 10 minutes PinmemberFrank T. van de Ven2:06 5 Jan '10  
QuestionThread List too? PinmemberLiamD2:47 10 Sep '09  
GeneralThanks! PinmemberScalee13:09 26 Aug '09  
Generalerror CS0029: Cannot implicitly convert type 'Terranova.API.ProcessInfo[]' to 'SmartCSD.Form1.ProcessInfo[]' Pinmembernohear_t7:44 8 Jul '09  
AnswerRe: error CS0029: Cannot implicitly convert type 'Terranova.API.ProcessInfo[]' to 'SmartCSD.Form1.ProcessInfo[]' PinmemberFrank T. van de Ven12:44 8 Jul '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120528.1 | Last Updated 30 May 2009
Article Copyright 2009 by Frank T. van de Ven
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid