简体   繁体   English

如何获取程序集的上次修改日期?

[英]How can I get the assembly last modified date?

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.我正在寻找类似的东西——我不想打开物理文件、获取它的属性或类似的东西,因为我将在母版页中呈现它,并且不想要那种开销.

I'll second pYrania's answer:我将第二个 pYrania 的回答:

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.所以不要,只需在应用程序加载事件中访问一次,然后将其存储为应用程序级变量。

If you default the Revision and Build Numbers in AssemblyInfo:如果您在 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);

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;

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)有些人认为 Assembly 不保存构建日期,但您知道它们错在哪里,您可以从嵌入在可执行文件中的PE 标头中检索链接器时间戳,如下所示可能有效(我自己没有测试过代码)

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.将此文件添加为资源,现在您的资源中有“BuildDate”字符串。

I have taken both answers from this question我已经从这个问题中得到了两个答案

The RetrieveLinkerTimestamp solution will not work after the year 2038 due to the use of int32 from 1970 .由于使用了1970int32RetrieveLinkerTimestamp解决方案在2038之后将不起作用。 I suggest using the following (although this probably has its limitations too):我建议使用以下内容(尽管这也可能有其局限性):

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

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.我相信获取此信息的唯一方法是通过文件句柄。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM