iox/ReflectionHelper.cs
/// <summary>
/// Get the linker date and time of an assembly.
/// </summary>
/// <returns>The linker date time.</returns>
/// <param name="assembly">The assembly.</param>
/// <param name="tzi">The time zone info.</param>
Get the linker date and time of an assembly.
Get the linker date and time of an assembly .
static DateTime GetLinkerDateTime (this Assembly assembly, TimeZoneInfo tzi = null) {
			const int PE_OFF_HEADER = 60;
			const int PE_OFF_LINKER_TS = 8;
			int offset;
			int linkerSecondsSince1970;
			try {
				var entryModule = assembly.ManifestModule;
				var hMod = Marshal.GetHINSTANCE (entryModule);
				if (hMod == IntPtr.Zero - 1) throw new Exception ("Failed to get HINSTANCE.");
				offset = Marshal.ReadInt32 (hMod, PE_OFF_HEADER);
				linkerSecondsSince1970 = Marshal.ReadInt32 (hMod, offset + PE_OFF_LINKER_TS);
			}
			catch {
				var buffer = new byte [2048];
				using (var stream = new FileStream (assembly.Location, FileMode.Open, FileAccess.Read)) {
					stream.Read (buffer, 0, 2048);
				}
				offset = BitConverter.ToInt32 (buffer, PE_OFF_HEADER);
				linkerSecondsSince1970 = BitConverter.ToInt32 (buffer, offset + PE_OFF_LINKER_TS);
			}
			var epoch = new DateTime (1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
			var linkTimeUtc = epoch.AddSeconds (linkerSecondsSince1970);
			var dt = TimeZoneInfo.ConvertTimeFromUtc (linkTimeUtc, tzi ?? TimeZoneInfo.Local);
			return dt;
		}
static DateTime GetLinkerDateTime ( this Assembly assembly , TimeZoneInfo tzi = null ) { const int PE_OFF_HEADER = 60 ; const int PE_OFF_LINKER_TS = 8 ; int offset ; int linkerSecondsSince1970 ; try { var entryModule = assembly . ManifestModule ; var hMod = Marshal . GetHINSTANCE ( entryModule ) ; if ( hMod == IntPtr . Zero - 1 ) throw new Exception ( " " ) ; offset = Marshal . ReadInt32 ( hMod , PE_OFF_HEADER ) ; linkerSecondsSince1970 = Marshal . ReadInt32 ( hMod , offset + PE_OFF_LINKER_TS ) ; } catch { var buffer = new byte [ 2048 ] ; using ( var stream = new FileStream ( assembly . Location , FileMode . Open , FileAccess . Read ) ) { stream . Read ( buffer , 0 , 2048 ) ; } offset = BitConverter . ToInt32 ( buffer , PE_OFF_HEADER ) ; linkerSecondsSince1970 = BitConverter . ToInt32 ( buffer , offset + PE_OFF_LINKER_TS ) ; } var epoch = new DateTime ( 1970 , 1 , 1 , 0 , 0 , 0 , DateTimeKind . Utc ) ; var linkTimeUtc = epoch . AddSeconds ( linkerSecondsSince1970 ) ; var dt = TimeZoneInfo . ConvertTimeFromUtc ( linkTimeUtc , tzi ?? TimeZoneInfo . Local ) ; return dt ; }
Get the linker date and time of an assembly.
Get the linker date and time of an assembly .
// PE constants // Declare variables // Try reading the linker time from memory // Find base memory address // Read the linker timestamp // Fall back to reading the linker time from disk // Read PE header // Read the linker timestamp // Convert the timestamp to a DateTime
The linker date time.
The linker date time .
Assembly
this
tzi
TimeZoneInfo
The time zone info.
The time zone info .
assembly
None