24

I would like to get the AssemblyCompany attribute from a WinForm project inside of my C# class library. In WinForms, I can get to this information by using:

Application.CompanyName;

However, I can't seem to find a way to get at that same information using a class library. Any help you could provide would be great!

Blake Blackwell
  • 7,575
  • 10
  • 50
  • 67
  • The AboutBox accesses and displays most of the Assembly Information (although not the Build Date). I just launch the AboutBox form, Minimised, to which I added some code to pop the required fields into a Common Class, and Close the Form. – italfingers Jan 05 '17 at 14:11

6 Answers6

31

To get the assembly in which your current code (the class library code) actually resides, and read its company attribute:

Assembly currentAssem = typeof(CurrentClass).Assembly;
object[] attribs = currentAssem.GetCustomAttributes(typeof(AssemblyCompanyAttribute), true);
if(attribs.Length > 0)
{
    string company = ((AssemblyCompanyAttribute)attribs[0]).Company
}
Rex M
  • 142,167
  • 33
  • 283
  • 313
  • I don't know if this is because I'm in .NET 2.0, but I had to add "true" to the end of call for GetCustomAttributes. Other than that, worked like a champ! Thanks for your help! – Blake Blackwell Oct 26 '09 at 19:37
  • 2
    I found it very useful! If you are using a new .NET version you can use typeof(CurrentClass).Assembly.GetCustomAttribute().Company – DarioDF Apr 14 '15 at 19:27
8

You could use the FileVersionInfo class to get CompanyName and much more.

Dim info = FileVersionInfo.GetVersionInfo(GetType(AboutPage).Assembly.Location)
Dim companyName = info.CompanyName
Dim copyright = info.LegalCopyright
Dim fileVersion = info.FileVersion
Pang
  • 9,564
  • 146
  • 81
  • 122
Mike Schall
  • 5,829
  • 4
  • 41
  • 47
  • Why would you use fileinfo to get information about the same assembly that's already in scope? – devlord Apr 16 '15 at 17:24
  • @lorddev The only way I see to get a FileVersionInfo object is by calling the static/shared GetVersionInfo function that takes a string path. Other options? – Mike Schall Apr 16 '15 at 21:01
4
Assembly assembly = typeof(CurrentClass).GetAssembly();
AssemblyCompanyAttribute companyAttribute = AssemblyCompanyAttribute.GetCustomAttribute(assembly, typeof(AssemblyCompanyAttribute)) as AssemblyCompanyAttribute;
if (companyAttribute != null)
{
    string companyName = companyAttribute.Company;
    // Do something
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Philip Wallace
  • 7,905
  • 3
  • 28
  • 40
2

The dotnet 5 version:

typeof(CurrentClass).GetCustomAttribute<AssemblyCompanyAttribute>().Company
eglasius
  • 35,831
  • 5
  • 65
  • 110
0

You can also get it using LINQ :

public static string GetAssemblyCompany<T>(){
    string company = (from Attribute a in (typeof(T).Assembly.GetCustomAttributes())
        where a is AssemblyCompanyAttribute
        select ((AssemblyCompanyAttribute)a).Company).FirstOrDefault();

    return company;
}
Jereck
  • 11
  • 3
0

This is a great way to get what you are looking for in a single line:

string company = ((AssemblyCompanyAttribute)Attribute.GetCustomAttribute(Assembly.GetExecutingAssembly(), typeof(AssemblyCompanyAttribute), false)).Company;
Randall Deetz
  • 512
  • 4
  • 25