Hi Bill,
If you are able / willing to use C# 2008, you can use LINQ to get the IDs like this:
Code:
var processIds = from p in System.Diagnostics.Process.GetProcesses()
where p.ProcessName.Contains("devenv")
select p.Id;
foreach (var processId in processIds)
{
Debug.WriteLine(String.Format("Process Id {0}", processId.ToString()));
}
The example use the where clause to limit the processes by their name but other criteria are possible as well. This gives you an IEnumerable<int> that you can loop through as the code demonstrates.
Alternatively, and less cool (but works in C# 2005), use GetProcessesByName.
Code:
Process[] processes = System.Diagnostics.Process.GetProcessesByName("devenv");
Both methods could result in multiple processes / IDs being returned, so if your application allows multiple instances, you need to take care of that as somehow.
Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of
ASP.NET 2.0 Instant Results and
Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.