Programmatically retrieving assembly version of a

2019-04-21 07:23发布

问题:

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:

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.



回答2:

Try this:

System.Reflection.Assembly.GetAssembly(typeof(ServiceController)).GetName().Version


回答3:

 Assembly runningAssembly = Assembly.GetEntryAssembly();
 if (runningAssembly == null)
 {
    runningAssembly = Assembly.GetExecutingAssembly();
 }
runningAssembly.GetName().Version;

Use this code inside you service.