Is it possible to retrieve this value at runtime?
I'd like to keep the value in one place, and retrieve it whenever my application needs to output the name of my company.
AssemblyInfo. cs contains information about your assembly, like name, description, version, etc. You can find more details about its content reading the comments that are included in it.
It is located under the folder "Properties". AssemblyInfo. cs file also get created, when you create any Web Application project.
You can generate an assemblyInfo. cs by right clicking the project and chosing properties. In the application tab fill in the details and press save, this will generate the assemblyInfo.
This should do it:
using System.Reflection; string company = ((AssemblyCompanyAttribute)Attribute.GetCustomAttribute( Assembly.GetExecutingAssembly(), typeof(AssemblyCompanyAttribute), false)) .Company;
If you really want to keep the value in one place and you have multiple assemblies in your solution, you could either:
GetEntryAssembly
instead of GetExecutingAssembly
and set the company attribute only on your entry assembly, or better:UPDATE Improved the code by suggestion of @hmemcpy so it doesn't need [0]
anymore. Thanks!
If you want a nice generic method for fetching the assembly Info data try the following:
public string GetAssemblyAttribute<T>(Func<T, string> value) where T : Attribute { T attribute = (T)Attribute.GetCustomAttribute(Assembly.GetExecutingAssembly(), typeof (T)); return value.Invoke(attribute); }
So you can then call
string title = GetAssemblyAttribute<AssemblyTitleAttribute>(a => a.Title); string copyright = GetAssemblyAttribute<AssemblyCopyrightAttribute>(a => a.Copyright); string version = GetAssemblyAttribute<AssemblyVersionAttribute>(a => a.Version); string description = GetAssemblyAttribute<AssemblyDescriptionAttribute>(a => a.Description);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With