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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; namespace SandBox { public partial class Form12 : Form { public Form12() { InitializeComponent(); } private void button1_Click( object sender, EventArgs e) { try { //string path = "Sandbox.exe"; string path = this .textBox1.Text; DateTime buildDateTime = this .GetBuildDateTime(path); MessageBox.Show( this , string .Format( "{0:yyyy'/'MM'/'dd HH':'mm':'ss}" , buildDateTime), this .Text, MessageBoxButtons.OK, MessageBoxIcon.Information); // 表示結果:"2012/09/11 10:53:19" } catch (Exception ex) { MessageBox.Show(ex.Message); } } /// <summary> /// アセンブリファイルのビルド日時を取得する。 /// </summary> /// <param name="asmPath">exeやdll等のアセンブリファイルのパス。 /// <returns>取得したビルド日時。</returns> private DateTime GetBuildDateTime( string asmPath) { // ファイルオープン using (FileStream fs = new FileStream(asmPath, FileMode.Open, FileAccess.Read)) using (BinaryReader br = new BinaryReader(fs)) { // まずはシグネチャを探す byte [] signature = { 0x50, 0x45, 0x00, 0x00 }; // "PE\0\0" List< byte > bytes = new List< byte >(); while ( true ) { bytes.Add(br.ReadByte()); if (bytes.Count < signature.Length) { continue ; } while (signature.Length < bytes.Count) { bytes.RemoveAt(0); } bool isMatch = true ; for ( int i = 0; i < signature.Length; i++) { if (signature[i] != bytes[i]) { isMatch = false ; break ; } } if (isMatch) { break ; } } // COFFファイルヘッダを読み取る var coff = new { Machine = br.ReadBytes(2), NumberOfSections = br.ReadBytes(2), TimeDateStamp = br.ReadBytes(4), PointerToSymbolTable = br.ReadBytes(4), NumberOfSymbols = br.ReadBytes(4), SizeOfOptionalHeader = br.ReadBytes(2), Characteristics = br.ReadBytes(2), }; // タイムスタンプをDateTimeに変換 int timestamp = BitConverter.ToInt32(coff.TimeDateStamp, 0); DateTime baseDateTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); DateTime buildDateTimeUtc = baseDateTime.AddSeconds(timestamp); DateTime buildDateTimeLocal = buildDateTimeUtc.ToLocalTime(); return buildDateTimeLocal; } } } } </ byte ></ byte > |
Author:シカハチ
へぼプログラマです。