I am looking to include a function in my DLL that gives the time of build, or an autobuild number. While are plenty of C# examples but I've haven't yet seen anything for F#. I would like to do this without external packages or a C# layer if possible.
Asked
Active
Viewed 203 times
2 Answers
2
Here you go. I ported the code from here:
module MyModule
type BuildTime =
static member RetrieveLinkerTimestamp() =
let c_PeHeaderOffset = 60
let c_LinkerTimestampOffset = 8
let filePath = System.Reflection.Assembly.GetCallingAssembly().Location
let b : byte array = Array.zeroCreate 2048
use s = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read)
s.Read(b, 0, 2048) |> ignore
let i = System.BitConverter.ToInt32(b, c_PeHeaderOffset)
let secondsSince1970 = System.BitConverter.ToInt32(b, i + c_LinkerTimestampOffset)
let dt = System.DateTime(1970, 1, 1, 0, 0, 0)
let dt1 = dt.AddSeconds(float(secondsSince1970))
dt1.AddHours(float(System.TimeZone.CurrentTimeZone.GetUtcOffset(dt1).Hours))
It's not pretty, but it works. I wrote it as a type so that it would be easily accessible from any other .NET code.
2
Last Modified
The following will tell you the last modified time of the assembly the function is located in
let lastModified() =
System.Reflection.Assembly.GetExecutingAssembly().Location
|> System.IO.File.GetLastWriteTime
Version
To get the build number, you first have to add it the the F# program (its not there in the default template.) Add this to a F# source file in your project (e.g. Assemblyinfo.fs)
module AssemblyInfo
open System.Reflection
[<assembly: AssemblyVersion("1.0.*")>]
()
Note however that version 1.0.* does not work like in C# (in 2010), it will just auto-increment the minor and not the revision. 1.0.0.* increments the revision but not the minor.
Then you can read the assembly version as follows:
let version() =
System.Reflection.Assembly
.GetExecutingAssembly()
.GetName()
.Version

Patrick McDonald
- 64,141
- 14
- 108
- 120