【问题标题】:Setting the value of a constant or variable at build time在构建时设置常量或变量的值
【发布时间】:2015-01-20 10:30:45
【问题描述】:

我想在我的应用程序启动代码中定义一个常量或字段,以反映应用程序实际构建的日期。例如:

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

这是否可以使用构建事件来完成,以便始终设置它并且我不必在积极构建发布版本之前主动记住这样做。

如果有可能怎么做?我已经阅读了有关构建事件的 msdn 文档,但似乎没有任何内容涵盖我想要尝试和做的事情。

【问题讨论】:

  • My.Settings 可以工作...
  • 在这种情况下如何工作。正如我所看到的,您最终会使用 ApplicationBuildDate = Now 之类的东西,它总是会解析为检查的日期而不是实际的构建日期。
  • 我不知道 VS 是否可以使用宏将 DateTimes 写为变量值(或将其放入资源文件中)。但是你可以have a look at this..
  • 该链接可能只是提供答案,+1 用于标记它...谢谢。

标签: vb.net visual-studio build-events


【解决方案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/4237809http://blog.codinghorror.com/determining-build-date-the-hard-way/一起抓来的

【讨论】:

  • YW 感谢您的反馈,当然也感谢原作者 :-)
猜你喜欢
  • 2016-04-21
  • 1970-01-01
  • 1970-01-01
  • 2016-09-07
  • 1970-01-01
  • 1970-01-01
  • 2012-06-06
  • 2021-11-01
  • 2020-04-14
相关资源
最近更新 更多