Answered by:
get the copyright property using C#

Question
-
I'm trying to figure out how to get the copyright property using C#. In Visual Basic, it is as easy as:
lblCopyright.Text = My.Application.Info.Copyright.ToString();
However, this is proving to be very frustrating and difficult in C#. I'm still new to C# and learning so please provide examples if possible.
Thank you.
Tuesday, April 30, 2013 12:47 AM
Answers
-
Hi,
Here goes a snippet I've found on the web:
private string GetCopyright() { Assembly asm = Assembly.GetExecutingAssembly(); object[] obj = asm.GetCustomAttributes(false); foreach (object o in obj) { if (o.GetType() == typeof(System.Reflection.AssemblyCopyrightAttribute)) { AssemblyCopyrightAttribute aca = (AssemblyCopyrightAttribute) o; return aca.Copyright; } } return string.Empty; }
Make sure you import namespace: System.Reflection
Hope this helps,
Please use Mark as Answer if my post solved your problem and use Vote As Helpful if a post was useful.
Pedro Martins
Portugal
https://www.linkedin.com/in/rechousa- Marked as answer by Daffitt Tuesday, April 30, 2013 7:24 PM
Tuesday, April 30, 2013 12:54 AM
All replies
-
Hi,
Here goes a snippet I've found on the web:
private string GetCopyright() { Assembly asm = Assembly.GetExecutingAssembly(); object[] obj = asm.GetCustomAttributes(false); foreach (object o in obj) { if (o.GetType() == typeof(System.Reflection.AssemblyCopyrightAttribute)) { AssemblyCopyrightAttribute aca = (AssemblyCopyrightAttribute) o; return aca.Copyright; } } return string.Empty; }
Make sure you import namespace: System.Reflection
Hope this helps,
Please use Mark as Answer if my post solved your problem and use Vote As Helpful if a post was useful.
Pedro Martins
Portugal
https://www.linkedin.com/in/rechousa- Marked as answer by Daffitt Tuesday, April 30, 2013 7:24 PM
Tuesday, April 30, 2013 12:54 AM -
Thank you for your help Rechousa. This was helpful. I noticed though that this method requires using a separate variation of the same method for each attribute in the Assembly CustomAttributes collection. It would really be cool if I could use one method that takes an argument for the attribute I want to retrieve and return the appropriate information. That way I would not have to write a different methods for each attribute I want to retrieve.
Thanks for your help.
Tuesday, April 30, 2013 7:29 PM -
There is actually a more elegant way to retrieve these attribute values.
string copyright, version;
Assembly asm = Assembly.GetExecutingAssembly();
copyright = ((AssemblyCopyrightAttribute)asm.GetCustomAttribute(typeof(AssemblyCopyrightAttribute))).Copyright;
version = ((AssemblyFileVersionAttribute)asm.GetCustomAttribute(typeof(AssemblyFileVersionAttribute))).Version;
Saturday, August 20, 2016 3:57 AM