C # How to get the version of this current executable .exe file

I use this code to get my version of programs:

public string progVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();

However, it does not always seem to capture the version I expect. I really don’t understand how it works completely or what it does.

I think this is passed because I run my program from another program, then it seems to grab the version of the program that ran it, or "GetExecutingAssembly ()" I assume links to the program that executed my programs, for example:

System.Diagnostics.Process.Start(my_programs_path);

Is there a more reliable way to get the version of a program for a real program at the time I ask for it?

Perhaps even run my program without leaving any path, as if the user himself had just launched it.

Thanks for any help!

+4
source share
2 answers

GetExecutingAssembly()returns an assembly containing the thaτ method that calls it. If you select it from the library, you will always get the version of the library, not the executable program.

To use the application executable GetEntryAssembly()

Consider the following example:

In AssemblyA:

class VersionTest
{
  void Test()
  {
    Console.Write("Executing assembly: {0}\n", Assembly.GetExecutingAssembly().GetName().ToString()); // returns AssemblyA
    Console.Write("Entry assembly: {0}\n", Assembly.GetEntryAssembly().GetName().ToString());         // returns AssemblyB
  }
}

In AssemblyB:

class Program
{
   void Main()
   {
      var ver = new VersionTest();
      ver.Test();
   }
}
0
source

You can use a property of a Assemblyknown type with the help typeofthat is defined in your application to make sure you get the "correct" assembly, and then extract a version of it, for example.

typeof(YourKnownType).Assembly.GetName().Version.ToString();
+2
source

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


All Articles