如何获取程序集的最后修改日期?

发布于 2024-07-19 00:49:14 字数 247 浏览 5 评论 0原文

我想渲染(用于内部调试/信息)程序集的最后修改日期,这样我就知道某个网站的部署时间。

通过反射可以得到吗?

我得到这样的版本:

Assembly.GetExecutingAssembly().GetName().Version.ToString();

我正在寻找类似的东西 - 我不想打开物理文件,获取其属性或类似的东西,因为我将在母版页中呈现它,并且不要不想要那种开销。

I want to render (for internal debugging/info) the last modified date of an assembly, so I'll know when a certain website was deployed.

Is it possible to get it through reflection?

I get the version like this:

Assembly.GetExecutingAssembly().GetName().Version.ToString();

I'm looking for something similar -- I don't want to open the physical file, get its properties, or something like that, as I'll be rendering it in the master page, and don't want that kind of overhead.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

℉絮湮 2024-07-26 00:49:14

我会赞同 pYrania 的答案:

System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.FileInfo fileInfo = new System.IO.FileInfo(assembly.Location);
DateTime lastModified = fileInfo.LastWriteTime;

但是添加以下内容:

您提到您不想访问文件系统,因为它位于您的母版页中,并且您不想为每个页面都点击额外的文件系统。 所以不要,只需在应用程序加载事件中访问它一次,然后将其存储为应用程序级变量即可。

I'll second pYrania's answer:

System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.FileInfo fileInfo = new System.IO.FileInfo(assembly.Location);
DateTime lastModified = fileInfo.LastWriteTime;

But add this:

You mention you don't want to access the file system since it's in your master page and you don't want to make that extra file system hit for every page. So don't, just access it once in the Application load event and then store it as an application-level variable.

错々过的事 2024-07-26 00:49:14

如果您在 AssemblyInfo 中默认修订版本号和内部版本号:

[assembly: AssemblyVersion("1.0.*")]

您可以通过以下方式获取大致的构建日期:

Version version = typeof(MyType).Assembly.GetName().Version;
DateTime date = new DateTime(2000, 1, 1)
    .AddDays(version.Build)
    .AddSeconds(version.Revision * 2);

If you default the Revision and Build Numbers in AssemblyInfo:

[assembly: AssemblyVersion("1.0.*")]

You can get the an approximate build date with:

Version version = typeof(MyType).Assembly.GetName().Version;
DateTime date = new DateTime(2000, 1, 1)
    .AddDays(version.Build)
    .AddSeconds(version.Revision * 2);
玩套路吗 2024-07-26 00:49:14

这个怎么样?

System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.FileInfo fileInfo = new System.IO.FileInfo(assembly.Location);
DateTime lastModified = fileInfo.LastWriteTime;

How about this?

System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.FileInfo fileInfo = new System.IO.FileInfo(assembly.Location);
DateTime lastModified = fileInfo.LastWriteTime;
风月客 2024-07-26 00:49:14

有些人认为 Assembly 不保留构建日期,但你知道他们错了,
您可以从嵌入在可执行文件中的 PE header 检索链接器时间戳,如下所示工作(我自己没有测试过代码)

private DateTime RetrieveLinkerTimestamp()
{
    string filePath = System.Reflection.Assembly.GetCallingAssembly().Location;
    const int c_PeHeaderOffset = 60;
    const int c_LinkerTimestampOffset = 8;
    byte[] b = new byte[2048];
    System.IO.Stream 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();
        }
    }

    int i = System.BitConverter.ToInt32(b, c_PeHeaderOffset);
    int secondsSince1970 = System.BitConverter.ToInt32(b, i + c_LinkerTimestampOffset);
    DateTime dt = new DateTime(1970, 1, 1, 0, 0, 0);
    dt = dt.AddSeconds(secondsSince1970);
    dt = dt.AddHours(TimeZone.CurrentTimeZone.GetUtcOffset(dt).Hours);
    return dt;
}

或者如果程序集是你自己的,你可以使用以下方法,简单易行,

在下面添加到预构建事件命令行:

echo %date% %time% > "$(ProjectDir)\Resources\BuildDate.txt"

将此文件添加为资源,现在你的文件中有“BuildDate”字符串资源。

我已经从这个问题中获取了两个答案

Some people think that Assembly doesn't holds build date but you know what they are wrong,
you can be retrieve the linker timestamp from the PE header embedded in the executable file, like following may work (i havn't tested the code myself)

private DateTime RetrieveLinkerTimestamp()
{
    string filePath = System.Reflection.Assembly.GetCallingAssembly().Location;
    const int c_PeHeaderOffset = 60;
    const int c_LinkerTimestampOffset = 8;
    byte[] b = new byte[2048];
    System.IO.Stream 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();
        }
    }

    int i = System.BitConverter.ToInt32(b, c_PeHeaderOffset);
    int secondsSince1970 = System.BitConverter.ToInt32(b, i + c_LinkerTimestampOffset);
    DateTime dt = new DateTime(1970, 1, 1, 0, 0, 0);
    dt = dt.AddSeconds(secondsSince1970);
    dt = dt.AddHours(TimeZone.CurrentTimeZone.GetUtcOffset(dt).Hours);
    return dt;
}

or if assembly is your's own better you use following approach simple and easy

Add below to pre-build event command line:

echo %date% %time% > "$(ProjectDir)\Resources\BuildDate.txt"

Add this file as resource, now you have 'BuildDate' string in your resources.

I have taken both answers from this question

╭ゆ眷念 2024-07-26 00:49:14

由于使用了 1970int32RetrieveLinkerTimestamp 解决方案在 2038 之后将不再工作。 我建议使用以下内容(尽管这可能也有其局限性):

IO.File.GetLastWriteTime(Reflection.Assembly.GetExecutingAssembly().Location)

The RetrieveLinkerTimestamp solution will not work after the year 2038 due to the use of int32 from 1970. I suggest using the following (although this probably has its limitations too):

IO.File.GetLastWriteTime(Reflection.Assembly.GetExecutingAssembly().Location)
很酷又爱笑 2024-07-26 00:49:14

我不相信程序集保存了最后修改的信息,因为这是操作系统属性。 我相信获取此信息的唯一方法是通过文件句柄。

I don't believe the assembly holds last modified information as that is an operating system attribute. I believe the only way to get this information is through a file handle.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文