[C#] 나 자신의 Build(Link) 시간을 알고 싶을 때

2013. 12. 27. 11:18Coders

출처 : StackOverflow


스크랩 같은 겁니다. 출처는 저 윗쪽에 링크 걸어 놨고요,

우리가 뭔가 프로그램을 만들 때, 또는 그 프로그램이 고객에게 배포되었을 때, 해당 파일이 언제 빌드 되었는지를 About 대화 상자에 표시 한다거나, 확인하고 싶을 때가 있습니다. 뭐 마우스 우클릭으로 파일 정보를 보는 방법도 있겠으나, 프로그램에서 처리하여 빌드 시간을 알고 싶어 좀 검색해 봐서 찾은 소스 입니다.


보면, 리플렉션을 통해 해당 어셈블리의 풀 패스를 얻어서 해당 파일을 열어 비트 단위로 막 어쩌고 저쩌고 @##$!#$!%!#$%...


그대로 퍼 온 겁니다.

private DateTime RetrieveLinkerTimestamp()
{
	string filePath = System.Reflection.Assembly.GetCallingAssembly().Location;
	const int c_PeHeaderOffset = 60;
	const int c_LinkerTimestampOffset = 8;
	byte[] b = new byte[2048];
	System.IO.Stream s = null;

	try
	{
		s = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
		s.Read(b, 0, 2048);
	}
	finally
	{
		if (s != null)
		{
			s.Close();
		}
	}

	int i = System.BitConverter.ToInt32(b, c_PeHeaderOffset);
	int secondsSince1970 = System.BitConverter.ToInt32(b, i + c_LinkerTimestampOffset);
	DateTime dt = new DateTime(1970, 1, 1, 0, 0, 0);
	dt = dt.AddSeconds(secondsSince1970);
	dt = dt.AddHours(TimeZone.CurrentTimeZone.GetUtcOffset(dt).Hours);
	return dt;
}