How to get version information for my Windows Service programmatically

I need to get the version of my Windows service programmatically and save it in a string. Then I will add a version to my display name and service name in the ProjectInstaller class. Now I get an empty string, and it’s hard for me to debug my installation project. Here is my current code:

        string version = null;
        try
        {
            Assembly exeAssembly = Assembly.GetEntryAssembly();
            Type attrType = typeof(AssemblyFileVersionAttribute);
            object[] attributes = exeAssembly.GetCustomAttributes(attrType, false);
            if (attributes.Length > 0)
            {
                AssemblyFileVersionAttribute verAttr = (AssemblyFileVersionAttribute)attributes[0];
                if (verAttr != null)
                {
                    version = verAttr.Version;
                }
            }
        }
        catch
        {
        }
        if (version == null)
        {
            version = string.empty;
        }
+3
source share
1 answer
var version = Assembly.GetExecutingAssembly().GetName().Version;
return version.ToString();

Will return it to 1.0.0.0.

Or you can use version.Major + "." + version.Minorto get only the first two numbers.

Alternatively, if you want a file version ...

var fvi = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location);
return fvi.FileVersion;
+8
source

Source: https://habr.com/ru/post/1749451/


All Articles