2014-11-22 63 views
0

我希望在应用程序启动代码中定义一个常量或字段,以反映实际构建应用程序的日期。喜欢的东西,例如:在编译时设置常量或变量的值

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

可以这样使用生成事件,以便它总是被设置来完成,我不会主动记住要做到这一点之前,积极建设一个发布版本。

如果有可能如何做到这一点?我已阅读关于构建事件的msdn文档,但似乎没有涵盖我想尝试和做的事情。

+0

我。设置将工作... – Codexer 2014-11-22 09:00:17

+0

这将如何在这种情况下工作。正如我所看到的那样,你最终会使用类似于ApplicationBuildDate = Now的东西,它将始终解析为它被检查的日期而不是实际的生成日期。 – 2014-11-22 09:06:46

+1

我不知道VS是否可以使用宏将DateTimes写入变量值(或将其放入资源文件中)。但你可以[看看这个..](http://blog.codinghorror.com/determining-build-date-the-hard-way/) – 2014-11-22 10:33:13

回答

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

嗨,罗伊,确​​实很好,非常感谢。 – 2014-11-22 14:21:28

+0

YW感谢您的反馈,并感谢原作者:-) – 2014-11-22 19:03:55