[C#] Version 자동 설정 및 BuildDateTime 구하기
2011. 4. 15. 11:47ㆍOthers/C# 일반
Assembly가 Build된 일시를 구하기 위한 방법을 설명 드립니다.
방법1: Version Text를 바탕으로 조회하기
- 먼저 [Properties] 폴더에 [AssemblyInf.cs]를 엽니다.
- 다음과 같은 부분을 찾습니다. 보통 최하단에 위치하고 있습니다.
1234// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion(
"1.0.0.0"
)]
[assembly: AssemblyFileVersion(
"1.0.0.0"
)]
- Version Text가 자동으로 생성되도록 하기 위해 아래와 같이 변경합시다.
1234// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion(
"1.0.*"
)]
[assembly: AssemblyFileVersion(
"1.0.*"
)]
- 프로젝트를 빌드하면 자동으로 생성되는 Version Text는 다음과 같은 규칙을 따릅니다.
공식을 알았으니 이제 Version Text를 읽어서 DateTime으로 변환해 봅시다.123456//Version=MajorVersion.MinorVersion.BuildNumber.RevisionNumber
//BuildNumber는 2000년 1월 1일을 기준으로 Build된 날짜까지의 총 일수로 설정됩니다.
//RevisionNumber는 자정을 기준으로 Build된 시간까지의 지나간 초(Second) 값으로 설정됩니다.
//생성된 Version Text 예
Version=1.0.4122.21378
123456789101112131415161718192021222324252627282930313233343536/// <summary>
/// Version Text로부터 Build된 일시를 구합니다.
/// </summary>
/// <returns></returns>
public
DateTime getBuildDateTime()
{
//1. Assembly.GetExecutingAssembly().FullName의 값은
//'ApplicationName, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'
//와 같다.
string
strVersionText = Assembly.GetExecutingAssembly().FullName
.Split(
','
)[1]
.Trim()
.Split(
'='
)[1];
//2. Version Text의 세번째 값(Build Number)은 2000년 1월 1일부터
//Build된 날짜까지의 총 일(Days) 수 이다.
int
intDays = Convert.ToInt32(strVersionText.Split(
'.'
)[2]);
DateTime refDate =
new
DateTime(2000, 1, 1);
DateTime dtBuildDate = refDate.AddDays(intDays);
//3. Verion Text의 네번째 값(Revision NUmber)은 자정으로부터 Build된
//시간까지의 지나간 초(Second) 값 이다.
int
intSeconds = Convert.ToInt32(strVersionText.Split(
'.'
)[3]);
intSeconds = intSeconds * 2;
dtBuildDate = dtBuildDate.AddSeconds(intSeconds);
//4. 시차조정
DaylightTime daylingTime = TimeZone.CurrentTimeZone
.GetDaylightChanges(dtBuildDate.Year);
if
(TimeZone.IsDaylightSavingTime(dtBuildDate, daylingTime))
dtBuildDate = dtBuildDate.Add(daylingTime.Delta);
return
dtBuildDate;
}
방법2: Assembly 파일의 작성시간을 조회하기
Assembly 파일자체의 작성시간을 조회하여 BuildDate를 구할 수 도 있습니다.
1 2 3 4 5 6 7 8 9 | /// <summary> /// Assembly의 Build된 일시를 구합니다. /// </summary> /// <returns></returns> public DateTime getBuildDateTime() { Assembly assembly = Assembly.GetExecutingAssembly(); return System.IO.File.GetLastWriteTime(assembly.Location); } |
Referneces
'Others > C# 일반' 카테고리의 다른 글
[C#] ANSI Encoding로 파일 저장하기 (0) | 2011.07.18 |
---|---|
[C#] File, Directory 보안 설정 (0) | 2011.04.23 |
[Office] Excel Html을 이용하여 Excel 문서 작성하기 (0) | 2011.02.28 |
[Office] XElement를 이용하여 Excel XML 작성하기 (0) | 2011.01.24 |
[Office.Interop] Server에서 Microsoft.Interop.Excel.dll 설정 (0) | 2011.01.20 |