Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
222 views
in Technique[技术] by (71.8m points)

c# - Compile date and time

Is there some clever way of getting the date and time of when the dll was built/compiled?

I’m using the assembly version numbering and reflection to retrieve and display this info when the app is deployed. But in some scenarios it would be more convenient to know when then dll was actually compiled rather than the auto incrementing version number. I don’t think the modified date on the dll file itself is reliable due to the way the app is deployed.

Dim assemblies = AppDomain.CurrentDomain.GetAssemblies
Dim assemblyName As String
Dim assemblyVersion As String

For Each assembly In assemblies
  assemblyName = assembly.GetName.Name.ToString
  assemblyVersion = assembly.GetName.Version.ToString
  ' How to get the date/time of the build??
  ' ...
Next

Any suggestions?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you set the assembly version (usually in AssemblyInfo.cs) to Major.Minor.* (e.g. 1.0.*), then you can probably retrieve the build date at runtime with something like this:

var version = Assembly.GetExecutingAssembly().GetName().Version;
DateTime buildDate = new DateTime(2000, 1, 1)
    .AddDays(version.Build)
    .AddSeconds(version.Revision*2);

When using a * for the third and fourth part of the assembly version, then these two parts are set automatically at compile time to the following values:

  • third part is the number of days since 2000-01-01
  • fourth part is the number of seconds since midnight divided by two (although some MSDN pages say it is a random number)

Oh, and you have to take care of daylight saving time yourself (e.g. add one hour if it's daylight saving time).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...