获取组装的公司名称和版权信息

Com*_*ser 27 .net c# .net-assembly

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

Ale*_*ria 47

您可以像这样使用FileVersionInfo:

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

var companyName = versionInfo.CompanyName;
Run Code Online (Sandbox Code Playgroud)

  • 此答案可能不适用于“单个文件”发布的程序集。(请参阅帮助:https://learn.microsoft.com/en-us/dotnet/core/deploying/single-file) (2认同)
  • 是的,只是让其他可能来这里寻找答案的人(比如我)更快地找到他们需要的东西。我花了几个小时才弄清楚为什么这在调试中有效,但在发布后却不起作用。 (2认同)

Geo*_*ett 10

这个公司名称的答案:

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

类似的版权.(使用AssemblyCopyrightAttribute).


Mik*_*scu 5

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

var attributes = Assembly.GetEntryAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);

var attribute = null;
if (attributes.Length > 0)
{
    attribute = attributes[0] as AssemblyCompanyAttribute;
}
Run Code Online (Sandbox Code Playgroud)