Getting the Version of my C# app?

后端 未结 3 1438
失恋的感觉
失恋的感觉 2021-02-03 22:11

I am working on desktop application. I have create a setup.

Ex. My Application. Version is 1.0.0.

I want to get the current version

相关标签:
3条回答
  • 2021-02-03 22:34

    The info you are looking for is in AssemblyInfo.cs.

    To access the info written in there at runtime you can use the System.Reflection.Assembly.

    Use System.Reflection.Assembly.GetExecutingAssembly() to get the assembly (that this line of code is in) or use System.Reflection.Assembly.GetEntryAssembly() to get the assembly your project started with (most likely this is your app).

    In multi-project solutions this is something to keep in mind!

    string version = Assembly.GetExecutingAssembly().GetName().Version.ToString()
    // returns 1.0.0.0
    

    Corresponding AssemblyInfo.cs:

    Corresponding EXE-properties:

    This may be important when working with InstallShield (see comments) !

    0 讨论(0)
  • 2021-02-03 22:34

    Get the version of a specific assembly:

    private const string AssemblyName = "MyAssembly"; // Name of your assembly
    
    public Version GetVersion()
    {
        // Get all the assemblies currently loaded in the application domain.
        Assembly[] assemblies = Thread.GetDomain().GetAssemblies();
    
        for (int i = 0; i < assemblies.Length; i++)
        {
            if (string.Compare(assemblies[i].GetName().Name, AssemblyName) == 0)
            {
                return assemblies[i].GetName().Version;
            }
        }
    
        return Assembly.GetExecutingAssembly().GetName().Version; // return current version assembly or return null;
    }
    
    0 讨论(0)
  • 2021-02-03 22:39
    System.Reflection.Assembly executingAssembly = System.Reflection.Assembly.GetExecutingAssembly();
    var fieVersionInfo = FileVersionInfo.GetVersionInfo(executingAssembly .Location);
    var version = fieVersionInfo.FileVersion;
    
    0 讨论(0)
提交回复
热议问题