0

I have a function, which returns the Assembly File Version of a C# project (in Visual Studio). The problem is, that it is not located in the main project, so instead of returning the version of the actual application, it returns the version of the project (assembly) that it is located in. I have tried using GetCallingAssembly() to get the version of the primary project instead, but without luck. And even if I got it to work, I could image that it breaks quite easily, if I chain my calls through multiple project. Is there a way to get the version of a project, by its name? For instance, if my main project is called SolutionName.ProjectName, and the primary window (it's a WPF project) is called MainWindow.xaml (if that makes any difference).

My current code:

private string GetFileVersion()
{
    System.Reflection.Assembly assembly = System.Reflection.Assembly.GetCallingAssembly();
    FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
    return fvi.FileVersion;
}
Jakob Busk Sørensen
  • 5,599
  • 7
  • 44
  • 96
  • FileVersionInfo.GetVersionInfo takes a string parameter, pass the absolute location and it should return the fileversion. – Rishabh Kumar Oct 12 '18 at 07:39
  • @RishabhKumar but the absolute path changes, if I move my program? Isn't there some way to use a name or relative path? – Jakob Busk Sørensen Oct 12 '18 at 07:44
  • If you want to make some logic for the path then try using System.IO.Directory.GetParent() on the root project directory. From there you should be able to construct some kind of logic to your required project. – Rishabh Kumar Oct 12 '18 at 07:48

1 Answers1

0

You could try to load the assembly manually by its name via Assembly.Load to recive the the AssemblyName Object. This will provide you some version informations.

If you want to get the the current executable, check this out

Felix Quehl
  • 744
  • 1
  • 9
  • 24