andrew4582

GetConsoleHeaderString

Sep 9th, 2011
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.84 KB | None | 0 0
  1. using System;
  2. using System.Globalization;
  3. using System.Reflection;
  4. using System.Text;
  5.  
  6. namespace ConsoleApplication6 {
  7.  
  8.     class Program {
  9.        
  10.         static void Main(string[] args) {
  11.             Console.Out.WriteLine(GetConsoleHeaderString());
  12.             Console.Out.WriteLine("See the '-?' command-line option for usage details.");
  13.         }
  14.  
  15.         private static string GetConsoleHeaderString() {
  16.             var description = string.Empty;
  17.             var copyright = string.Empty;
  18.             var product = string.Empty;
  19.  
  20.             var assembly = Assembly.GetExecutingAssembly();
  21.             foreach(var attr in assembly.GetCustomAttributes(false)) {
  22.                 var attrType = attr.GetType();
  23.                 if(attrType == typeof(AssemblyDescriptionAttribute)) {
  24.                     description = ((AssemblyDescriptionAttribute)attr).Description;
  25.                 }
  26.                 else if(attrType == typeof(AssemblyCopyrightAttribute)) {
  27.                     copyright = ((AssemblyCopyrightAttribute)attr).Copyright;
  28.                     copyright = copyright.Replace("©","(c)");
  29.                 }
  30.                 else if(attrType == typeof(AssemblyProductAttribute)) {
  31.                     product = ((AssemblyProductAttribute)attr).Product;
  32.                 }
  33.             }
  34.  
  35.             var assemblyName = assembly.GetName();
  36.  
  37.             // combine the information for output
  38.             var sb = new StringBuilder();
  39.             sb.AppendLine(string.Format(CultureInfo.InvariantCulture,"{0} (version {1})",string.IsNullOrEmpty(product) ? assemblyName.Name : product,assemblyName.Version));
  40.             if(!string.IsNullOrEmpty(description)) { sb.AppendLine(description); }
  41.             if(!string.IsNullOrEmpty(copyright)) { sb.AppendLine(copyright); }
  42.             return sb.ToString();
  43.         }
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment