C# 获取 .NET 程序集的日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2050396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting the date of a .NET assembly
提问by DenaliHardtail
How can I retrieve the Created date from the current .NET assembly?
如何从当前的 .NET 程序集中检索创建日期?
I'd like to add some realy simple functionality where my app stops working one week after the build date of the main assembly. I already wrote the code that kills my app after a given date. I just need to programmatically retrieve the creation date from the assembly.
我想添加一些非常简单的功能,我的应用程序在主程序集的构建日期一周后停止工作。我已经编写了在给定日期后杀死我的应用程序的代码。我只需要以编程方式从程序集中检索创建日期。
采纳答案by Rob Levine
I don't think the assembly itself contains it's creation date. I suspect the closest you can get is the creation date of the assembly file itself:
我不认为程序集本身包含它的创建日期。我怀疑你能得到的最接近的是程序集文件本身的创建日期:
File.GetCreationTime(Assembly.GetExecutingAssembly().Location)
should do the trick.
应该做的伎俩。
EDIT:
编辑:
I think Jeff Atwood's solution, written up by "grenade" in this thread, is probably the better way to go now.
我认为 Jeff Atwood 的解决方案,在这个线程中由“手榴弹”编写,现在可能是更好的方法。
回答by Jake Pearson
This should work:
这应该有效:
var entryAssembly = Assembly.GetEntryAssembly();
var fileInfo = new FileInfo(entryAssembly.Location);
var buildDate = fileInfo.LastWriteTime;
回答by Wim Hollebrandse
What's wrong with:
有什么问题:
System.IO.File.GetLastWriteTime(Assembly.GetExecutingAssembly().Location);
回答by Paulo Santos
The best way to do this would be with a custom attribute that you set on the PreBuild
of your assembly.
执行此操作的最佳方法是使用您在PreBuild
程序集的 上设置的自定义属性。
And then use the standard reflection to get the attribute you created.
然后使用标准反射来获取您创建的属性。
But out of curiosity, why kill the app after the BUILD date?
但是出于好奇,为什么要在 BUILD 日期之后杀死该应用程序?
回答by jmlumpkin
Maybe this post on coding horrormay help
也许这篇关于编码恐怖的帖子可能会有所帮助
回答by grenade
The following is based on: https://blog.codinghorror.com/determining-build-date-the-hard-way/
以下内容基于: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);
}
}
回答by realbart
If you're writing an application for a mobile device using the compact framwork, Assembly.Location is not available.
如果您正在使用紧凑框架为移动设备编写应用程序,则 Assembly.Location 不可用。
Here, I found an alternative:
在这里,我找到了一个替代方案:
System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)