【发布时间】:2011-01-04 06:10:27
【问题描述】:
如何从当前的 .NET 程序集中检索 Created 日期?
我想添加一些非常简单的功能,让我的应用在主程序集的构建日期一周后停止工作。我已经编写了在给定日期后杀死我的应用程序的代码。我只需要以编程方式从程序集中检索创建日期。
【问题讨论】:
如何从当前的 .NET 程序集中检索 Created 日期?
我想添加一些非常简单的功能,让我的应用在主程序集的构建日期一周后停止工作。我已经编写了在给定日期后杀死我的应用程序的代码。我只需要以编程方式从程序集中检索创建日期。
【问题讨论】:
这应该可行:
var entryAssembly = Assembly.GetEntryAssembly();
var fileInfo = new FileInfo(entryAssembly.Location);
var buildDate = fileInfo.LastWriteTime;
【讨论】:
有什么问题:
System.IO.File.GetLastWriteTime(Assembly.GetExecutingAssembly().Location);
【讨论】:
GetCreationTime() 返回第一次创建程序集的时间。这不会返回上次创建程序集的时间。
我不认为程序集本身包含它的创建日期。我怀疑你能得到的最接近的是程序集文件本身的创建日期:
File.GetCreationTime(Assembly.GetExecutingAssembly().Location)
应该可以解决问题。
编辑:
我认为 Jeff Atwood 的解决方案(由“手榴弹”在此线程中编写)可能是现在更好的方法。
【讨论】:
GetLastWriteTime() 会更准确地了解它的构建时间。 GetCreationTime() 只会返回清理项目后创建的第一个构建
最好的方法是使用您在程序集的PreBuild 上设置的自定义属性。
然后使用标准反射获取你创建的属性。
但出于好奇,为什么要在 BUILD 日期之后终止应用?
【讨论】:
也许这个帖子on coding horror 可能会有所帮助
【讨论】:
以下基于:https://blog.codinghorror.com/determining-build-date-the-hard-way/
public static class ApplicationInformation
{
/// <summary>
/// Gets the executing assembly.
/// </summary>
/// <value>The executing assembly.</value>
public static System.Reflection.Assembly ExecutingAssembly
{
get { return executingAssembly ?? (executingAssembly = System.Reflection.Assembly.GetExecutingAssembly()); }
}
private static System.Reflection.Assembly executingAssembly;
/// <summary>
/// Gets the executing assembly version.
/// </summary>
/// <value>The executing assembly version.</value>
public static System.Version ExecutingAssemblyVersion
{
get { return executingAssemblyVersion ?? (executingAssemblyVersion = ExecutingAssembly.GetName().Version); }
}
private static System.Version executingAssemblyVersion;
/// <summary>
/// Gets the compile date of the currently executing assembly.
/// </summary>
/// <value>The compile date.</value>
public static System.DateTime CompileDate
{
get
{
if (!compileDate.HasValue)
compileDate = RetrieveLinkerTimestamp(ExecutingAssembly.Location);
return compileDate ?? new System.DateTime();
}
}
private static System.DateTime? compileDate;
/// <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 static System.DateTime RetrieveLinkerTimestamp(string filePath)
{
const int peHeaderOffset = 60;
const int linkerTimestampOffset = 8;
var b = new byte[2048];
System.IO.FileStream s = null;
try
{
s = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
s.Read(b, 0, 2048);
}
finally
{
if(s != null)
s.Close();
}
var 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);
}
}
【讨论】:
如果您正在使用紧凑型框架为移动设备编写应用程序,则 Assembly.Location 不可用。
Here,我找到了替代方案:
System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)
【讨论】: