Programmatically retrieving assembly version of a running service

岁酱吖の 提交于 2019-12-05 01:38:00

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.

Try this:

System.Reflection.Assembly.GetAssembly(typeof(ServiceController)).GetName().Version
 Assembly runningAssembly = Assembly.GetEntryAssembly();
 if (runningAssembly == null)
 {
    runningAssembly = Assembly.GetExecutingAssembly();
 }
runningAssembly.GetName().Version;

Use this code inside you service.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!