Search code examples
c#asp.net-coreversion.net-6.0publish

How to get project data (product, version) for published release in net 6


It is hard to get project data (product, version) in case if project was build as Published Release (In VS: Build -> Publish Selection). This way not working

var assembly = Assembly.GetExecutingAssembly();
var version = FileVersionInfo.GetVersionInfo(assembly.Location);

because have no access to Assembly. I found solution

GetType().Assembly.GetName().Version.ToString();

but this way I can get just version not other project attributes (Company, Product). How can I get all project data? Maybe need to some configure publish?

Once again: enter image description here Assembly NOT worked (unavailable) in case of Published Release but all OK on debug or run from code

P.S. Publish settings enter image description here

P.P.S. Try to run on "old" code enter image description here Push F10 enter image description here

Looks like it try to execute next line

var version = FileVersionInfo.GetVersionInfo(assembly.Location);

Solution

  • Looks like this is working

    var version = FileVersionInfo.GetVersionInfo(Environment.GetCommandLineArgs()[0]);
    

    or

    var version = FileVersionInfo.GetVersionInfo(Environment.ProcessPath);
    

    Found in this article https://learn.microsoft.com/en-us/dotnet/core/deploying/single-file/overview?tabs=vs#api-incompatibility