简体   繁体   English

在不是主程序集的程序集中获取正在运行的应用程序的名称和版本

[英]Getting the name and version of running application inside an assembly which is not the main assembly

I have an application which uses some other libraries which are implemented in their own assemblies. 我有一个使用其他一些库的应用程序,这些库在自己的程序集中实现。

In one of the assemblies I want to get the version of main application and not the running assembly. 在其中一个程序集中,我想获取主应用程序的版本,而不是正在运行的程序集。

The following code returns version of library and not version of main application: 以下代码返回库的版本,而不是主应用程序的版本:

   var lcAssembly = Assembly.GetExecutingAssembly();
   var parts = lcAssembly.FullName.Split(',');
   var lcVersionStr = parts[1].Split('=')[1];
   mVersion = new Version(lcVersionStr);

How can I get the version of main assembly? 我如何获得主机的版本?

很简单:

Assembly.GetEntryAssembly().GetName().Version

Use Assembly.GetEntryAssembly that will get you process executable in the default application domain. 使用Assembly.GetEntryAssembly将使您在默认应用程序域中处理可执行文件。

And just to mention that this will not work for ASP.NET applications 只需提及这将不适用于ASP.NET应用程序

See this answer: https://stackoverflow.com/a/4803419/91619 看到这个答案: https : //stackoverflow.com/a/4803419/91619

It provides a Nuget package which can provide the main application version for ASP.NET applications or other applications. 它提供了一个Nuget程序包,可以为ASP.NET应用程序或其他应用程序提供主要的应用程序版本。

https://nuget.org/packages/ApplicationAssemblyUtility https://nuget.org/packages/ApplicationAssemblyUtility

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM