views:

147

answers:

3

hello, i need to write a program in c# to collect information about processes running on my computer. I successfuly get the processes's names and ID using this code:

... Process[] processlist; ... foreach (Process theprocess in processlist) { Console.WriteLine(theprocess.ProcessName + ...) } ...

But i couldn't get the version of the processes (like Firefox or Visual Studio). does anybody know how to get the version of a running process? thanks a lot!

+2  A: 

theProcess.MainModule.FileVersionInfo. But there are one or two exceptions you need to catch.

Daniel Ives
A: 

You can get the filename of the executable from Process.StartInfo.FileName. Get a new instance of FileVersionInfo using the filename like this:

FileVersionInfo myFileVersionInfo = FileVersionInfo.GetVersionInfo(Process.StartInfo.FileName);

Then access myFileVersionInfo.FileVersion to get the file version.

EDIT: Daniel's way seems more efficient; I didn't know you could just access the loaded module's FileVersionInfo directly..

andyp
+1  A: 

Keep in mind that the executable's FileVersion and/or ProductVersion may not necessarily be the same as the application's version. For example, Firefox 3.5.3 reports a FileVersion of 1.9.1.3523, but a ProductVersion of 3.5.3. VMware Player 2.5.1, on the other hand, reports a FileVersion of 6.5.1.5078 and a ProductVersion of 6.5.1 build-126130. Depending on what you are using the information for, this may or may not be an issue.

Marty Dill