2014-11-22 5 views
0

응용 프로그램이 실제로 빌드 된 날짜를 반영하는 응용 프로그램 시작 코드에 상수 또는 필드를 정의하고 싶습니다. 예를 들어 같은 뭔가 :빌드시 상수 또는 변수 값 설정

Private Shared ApplicationBuiltOn As Date = <and here is where I would like to set the build date>` 

이가이 항상 설정되도록 빌드 이벤트를 사용하여 수행 할 수 있습니다 내가 적극적으로 전에 적극적으로 릴리스 버전을 구축하려면 어떻게 기억할 필요가 없습니다.

가능하면 어떻게 할 수 있습니까? 빌드 이벤트에 대한 msdn 문서를 읽었지만 아무것도 시도하고 싶지 않은 것 같습니다.

+0

내를. 설정이 작동합니다 ... – Codexer

+0

어떻게 이런 상황에서 작동합니다. ApplicationBuildDate = Now와 같은 것을 사용하면 실제로 빌드 날짜가 아닌 날짜로 해석됩니다. –

+1

VS가 매크로를 사용하여 가변 값으로 DateTimes를 쓸 수 있는지 (또는 ressource 파일에 넣을 수 있는지) 알 수 없습니다. 하지만 당신은 [이것 좀 봐 ...] (http://blog.codinghorror.com/determining-build-date-the-hard-way/) –

답변

1

당신이 시도 할 수 있었다 (그것을 테스트하고 나를 위해 일한)

Public NotInheritable Class ApplicationInformation 
    Private Sub New() 
    End Sub 
    ''' <summary> 
    ''' Gets the executing assembly. 
    ''' </summary> 
    ''' <value>The executing assembly.</value> 
    Public Shared ReadOnly Property ExecutingAssembly() As System.Reflection.Assembly 
     Get 
      Return If(m_executingAssembly, (InlineAssignHelper(m_executingAssembly, System.Reflection.Assembly.GetExecutingAssembly()))) 
     End Get 
    End Property 
    Private Shared m_executingAssembly As System.Reflection.Assembly 

    ''' <summary> 
    ''' Gets the executing assembly version. 
    ''' </summary> 
    ''' <value>The executing assembly version.</value> 
    Public Shared ReadOnly Property ExecutingAssemblyVersion() As System.Version 
     Get 
      Return If(m_executingAssemblyVersion, (InlineAssignHelper(m_executingAssemblyVersion, ExecutingAssembly.GetName().Version))) 
     End Get 
    End Property 
    Private Shared m_executingAssemblyVersion As System.Version 

    ''' <summary> 
    ''' Gets the compile date of the currently executing assembly. 
    ''' </summary> 
    ''' <value>The compile date.</value> 
    Public Shared ReadOnly Property CompileDate() As System.DateTime 
     Get 
      If Not m_compileDate.HasValue Then 
       m_compileDate = RetrieveLinkerTimestamp(ExecutingAssembly.Location) 
      End If 
      Return If(m_compileDate, New System.DateTime()) 
     End Get 
    End Property 
    Private Shared m_compileDate As System.Nullable(Of System.DateTime) 

    ''' <summary> 
    ''' Retrieves the linker timestamp. 
    ''' </summary> 
    ''' <param name="filePath">The file path.</param> 
    ''' <returns></returns> 
    ''' <remarks>http://www.codinghorror.com/blog/2005/04/determining-build-date-the-hard-way.html</remarks> 
    Private Shared Function RetrieveLinkerTimestamp(filePath As String) As System.DateTime 
     Const peHeaderOffset As Integer = 60 
     Const linkerTimestampOffset As Integer = 8 
     Dim b = New Byte(2047) {} 
     Dim s As System.IO.FileStream = Nothing 
     Try 
      s = New System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read) 
      s.Read(b, 0, 2048) 
     Finally 
      If s IsNot Nothing Then 
       s.Close() 
      End If 
     End Try 
     Dim dt = New System.DateTime(1970, 1, 1, 0, 0, 0).AddSeconds(System.BitConverter.ToInt32(b, System.BitConverter.ToInt32(b, peHeaderOffset) + linkerTimestampOffset)) 
     Return dt.AddHours(System.TimeZone.CurrentTimeZone.GetUtcOffset(dt).Hours) 
    End Function 
    Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, value As T) As T 
     target = value 
     Return value 
    End Function 
End Class 

을 그리고 다음과 같이 사용 :

Messagebox.Show("This application was built on: " & ApplicationInformation.CompileDate.ToString()) 

참고 : https://stackoverflow.com/a/3634544/4237809에서 함께 집어 들고 및 http://blog.codinghorror.com/determining-build-date-the-hard-way/

+0

안녕하세요, Roy는 매우 친절하게 일합니다. 많은 감사드립니다. –

+0

YW는 원본 저자 (들)에게 감사와 감사를드립니다 :-) –