.NET Getting and Setting the Application Version Number and other application attributes


/ Published in: C#
Save to your folder(s)

If you are just looking for the version information in the assembly, you can use `Application.ProductVersion` (at least for GUI applications). I don't think you'll get the reflection performance hit this way.

You can also add an "About" box to your GUI and see what code is generated for all of that information (product name, copyright, version, etc).


Copy this code and paste it in your HTML
  1. To retrieve the version number from the assembly in your code, you use can do this:
  2.  
  3. String strVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
  4.  
  5. To retrieve the version number from the assembly that is calling your assembly, you can use this:
  6.  
  7. String strVersion = System.Reflection.Assembly.GetCallingAssembly().GetName().Version.ToString();
  8.  
  9.  
  10.  
  11. // Code generated by adding an "About" box
  12. partial class AboutBox : Form
  13. {
  14. public AboutBox()
  15. {
  16. InitializeComponent();
  17. this.Text = AssemblyTitle;
  18. this.labelProductName.Text = AssemblyProduct;
  19. this.labelVersion.Text = String.Format("Version {0}", AssemblyVersion);
  20. this.labelCopyright.Text = AssemblyCopyright;
  21. this.labelCompanyName.Text = AssemblyCompany;
  22. this.textBoxDescription.Text = AssemblyDescription;
  23. }
  24.  
  25. #region Assembly Attribute Accessors
  26.  
  27. public string AssemblyTitle
  28. {
  29. get
  30. {
  31. object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
  32. if (attributes.Length > 0)
  33. {
  34. AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0];
  35. if (titleAttribute.Title != "")
  36. {
  37. return titleAttribute.Title;
  38. }
  39. }
  40. return System.IO.Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase);
  41. }
  42. }
  43.  
  44. public string AssemblyVersion
  45. {
  46. get
  47. {
  48. return Assembly.GetExecutingAssembly().GetName().Version.ToString();
  49. }
  50. }
  51.  
  52. public string AssemblyDescription
  53. {
  54. get
  55. {
  56. object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);
  57. if (attributes.Length == 0)
  58. {
  59. return "";
  60. }
  61. return ((AssemblyDescriptionAttribute)attributes[0]).Description;
  62. }
  63. }
  64.  
  65. public string AssemblyProduct
  66. {
  67. get
  68. {
  69. object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false);
  70. if (attributes.Length == 0)
  71. {
  72. return "";
  73. }
  74. return ((AssemblyProductAttribute)attributes[0]).Product;
  75. }
  76. }
  77.  
  78. public string AssemblyCopyright
  79. {
  80. get
  81. {
  82. object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
  83. if (attributes.Length == 0)
  84. {
  85. return "";
  86. }
  87. return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
  88. }
  89. }
  90.  
  91. public string AssemblyCompany
  92. {
  93. get
  94. {
  95. object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
  96. if (attributes.Length == 0)
  97. {
  98. return "";
  99. }
  100. return ((AssemblyCompanyAttribute)attributes[0]).Company;
  101. }
  102. }
  103. #endregion
  104. }

URL: http://www.xoc.net/works/tips/version.asp

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.