【问题标题】:Get company name and copyright information of assembly [duplicate]获取装配的公司名称和版权信息[重复]
【发布时间】:2013-10-23 10:23:12
【问题描述】:

我正在使用Assembly.GetEntryAssembly().GetName() 获取应用程序/程序集名称及其版本,但我没有看到公司名称和版权的任何变量。我怎么得到它?

【问题讨论】:

标签: c# .net .net-assembly


【解决方案1】:

你可以像这样使用FileVersionInfo

var versionInfo = FileVersionInfo.GetVersionInfo(Assembly.GetEntryAssembly().Location);

var companyName = versionInfo.CompanyName;

【讨论】:

  • 此答案可能不适用于“单个文件”发布的程序集。 (请参阅:docs.microsoft.com/en-us/dotnet/core/deploying/single-file
  • 也许,答案主要是针对完整(旧).NET Framework。
  • 是的,只是让其他可能来这里寻找答案的人(比如我)更快地找到他们需要的东西。我花了几个小时才弄清楚为什么这在调试中有效,但在发布后却没有。
【解决方案2】:

来自this answer的公司名称:

Assembly currentAssem = typeof(CurrentClass).Assembly;
object[] attribs = currentAssem.GetCustomAttributes(typeof(AssemblyCompanyAttribute), true);
if(attribs.Length > 0)
{
    string company = ((AssemblyCompanyAttribute)attribs[0]).Company
}

版权类似。 (使用AssemblyCopyrightAttribute)。

【讨论】:

    【解决方案3】:

    这些是您必须使用反射在 Assembly 对象上枚举的属性。

    var attributes = Assembly.GetEntryAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
    
    var attribute = null;
    if (attributes.Length > 0)
    {
        attribute = attributes[0] as AssemblyCompanyAttribute;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 2016-09-27
      • 2012-04-01
      • 2017-11-27
      • 1970-01-01
      • 1970-01-01
      • 2019-06-03
      相关资源
      最近更新 更多