views:

399

answers:

3

Hello!

I'd like to access to assembly version information of a service I "control" with ServiceController class. (ie. I'd like to display "2.3.1.23" ), however I can't find any information about retrieving assembly versions ... Is it possible at all?

EDIT: Just to clarify ... I only know the name of the service running on the local computer. I want to access the "FileVersionInfo" of that service (better said service exe), however I don't know where is that service exe located.

+1  A: 

Try this:

System.Reflection.Assembly.GetAssembly(typeof(ServiceController)).GetName().Version
Andrew Hare
I think the OP does not want to see the version of the .NET Assembly that implements the ServiceController class, but the version info for some exe file...
Thorsten Dittmar
+6  A: 

If I understand you correctly, you want to get the version of any service exe. Assuming that you know the name and path of the service's executable, you might want to try:

FileVersionInfo versionInfo = FileVersionInfo.GetVersionInfo(<path and name of service exe>);

You can then use the properties of the FileVersionInfo class to show the version number. Please note that this also works for UNC paths as long as you have permissions to read-access the file.

EDIT
To get the executable path and name if you only know the service name, you can access the Registry under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services. Search a key that matches the service name. Under that key, there's a value name ImagePath that contains the executable's name and path.

Thorsten Dittmar
Yes you understood correctly, and this would certanly work, however I don't know where the service exe is located ... I only know the name that is passed to the constructor of the ServiceController class.
Rekreativc
Edited my reply to show you how to get the executable for the service.
Thorsten Dittmar
A: 
 Assembly runningAssembly = Assembly.GetEntryAssembly();
 if (runningAssembly == null)
 {
    runningAssembly = Assembly.GetExecutingAssembly();
 }
runningAssembly.GetName().Version;

Use this code inside you service.

Pratik
Sorry, I thought you wanted to get the assembly version from code inside the running service.
Pratik