-
Notifications
You must be signed in to change notification settings - Fork 7
/
ProgramMeta.cs
87 lines (74 loc) · 2.09 KB
/
ProgramMeta.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.IO;
namespace Hosts;
static class ProgramMeta
{
public static T GetAssemblyAttribute<T>()
{
object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(T), false);
return (attributes.Length == 0) ? default(T) : (T)attributes[0];
}
public static string GetTitle()
{
var attr = GetAssemblyAttribute<AssemblyTitleAttribute>();
if (attr == null) return String.Empty;
var title = attr.Title;
var version = GetVersion();
if (version != null)
{
title += " v" + version;
}
#if DEBUG
title += " DEBUG";
#endif
var date = GetBuildDate();
if (date != DateTime.MinValue)
{
// For some reason, it replaces "yyyy/MM/dd" by a locale specific date format
// So, we use "-" as a delimiter and replace it to "/"
title += date.ToString(" [yyyy-MM-dd]").Replace('-', '/');
}
return title;
}
public static string GetVersion()
{
var attr = GetAssemblyAttribute<AssemblyFileVersionAttribute>();
if (attr == null) return null;
var parts = new List<string>(attr.Version.TrimEnd('0', '.').Split('.'));
if (parts.Count == 0) return null;
while (parts.Count < 3)
{
parts.Add("0");
}
return String.Join(".", parts.ToArray());
}
public static DateTime GetBuildDate()
{
try
{
// Read it from the PE header
var buffer = File.ReadAllBytes(Assembly.GetExecutingAssembly().Location);
var pe = BitConverter.ToInt32(buffer, 0x3C);
var time = BitConverter.ToInt32(buffer, pe + 8);
return (new DateTime(1970, 1, 1, 0, 0, 0, 0)).AddSeconds(time);
}
catch
{
return DateTime.MinValue;
}
}
public static string GetCopyright()
{
var attr = GetAssemblyAttribute<AssemblyCopyrightAttribute>();
return (attr == null) ? String.Empty : ("(C) " + attr.Copyright);
}
public static string GetDescription()
{
var attr = GetAssemblyAttribute<AssemblyDescriptionAttribute>();
return (attr == null) ? String.Empty : attr.Description;
}
}