如果 .NET 中较新,则覆盖文件

发布于 2024-09-11 01:41:50 字数 886 浏览 4 评论 0 原文

我的应用程序在启动时检查共享网络驱动器上是否有较新版本的文件。如果网络共享上的应用程序较新,则会将其复制到本地应用程序目录并覆盖旧的。我当前执行此操作的代码如下所示:

FileInfo sourceFile = new FileInfo(source + "\\" + fileName);
if(sourceFile.Exists) {
    FileInfo destFile = new FileInfo(destination + "\\" + fileName);
    if (destFile.Exists && destFile.LastWriteTime >= sourceFile.LastWriteTime)
    {
        //Using log4net
        log.Info("Current " + fileName + " is newer than the one on the server.");
        return false;
    }
}

检查日志,似乎有时源文件的 LastWriteTime 没有被检测为较新的文件(当它是较新的事件时)。我可能会混淆写入时间和修改时间吗?有人知道如何实现这一目标吗?

编辑(以及下面我的评论的副本): 我正在复制的文件大部分是我可以控制的 DLL 文件。它们是应用程序“Growl for Windows”的程序集;他们定义自定义显示。我的应用程序的目的是检查网络共享以查看是否有可用的较新版本,并在需要时在本地复制该版本。这样,我们可以帮助确保所有客户都使用最新的显示器。

编辑2:

好的,我在加载程序集时胡思乱想,然后遇到了另一个问题。当通过 Assembly.ReflectionOnlyLoadFrom 加载同一文件时,即使它们来自不同的位置,您似乎也无法加载两次。

My application checks on startup if there is a newer version of a file on a shared network drive. If the one on the network share is newer it copies it to the local application directory and overwrites the old one. My current code to do this goes something like this:

FileInfo sourceFile = new FileInfo(source + "\\" + fileName);
if(sourceFile.Exists) {
    FileInfo destFile = new FileInfo(destination + "\\" + fileName);
    if (destFile.Exists && destFile.LastWriteTime >= sourceFile.LastWriteTime)
    {
        //Using log4net
        log.Info("Current " + fileName + " is newer than the one on the server.");
        return false;
    }
}

Checking the logs, it seems that sometimes the LastWriteTime of the source file is not being detected as newer (event when it is). Am I possibly getting write times and modified times confused? Does anybody have an idea of how to achieve this?

EDIT (and copy of my comment below):
The files I am copying are mostly DLL files which I have control over. They are assemblies for the application "Growl for Windows"; they define custom displays. The intent of my application is to check the network share to see if there is a newer version available and to copy that locally if required. That way, we can help to ensure that all our clients are using the most up-to-date display.

EDIT 2:

OK, I fooled around with loading the assemblies, and I ran into another problem. It seems you cannot load the same file twice, even if they are from different locations, when they're loaded via Assembly.ReflectionOnlyLoadFrom.

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

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

发布评论

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

评论(2

皇甫轩 2024-09-18 01:41:50

LastWriteTime 文档确实注意到返回的值可能不准确。请注意有关 Refresh 方法的注释 - 虽然上面的代码几乎肯定不需要调用此方法,但如果您要创建 FileInfo 对象并保留它们任意时间长度,则这可能是适用的。

Windows 可以随意延迟更新文件的实际上次写入时间 - 至于可以延迟多长时间,请检查处理文件信息的 Windows API 调用。

The docs for LastWriteTime do note that the value returned may not be accurate. Note the comment about the Refresh method as well - whilst the code above would almost certainly not require this to be called, if you're creating FileInfo objects and holding onto them for any length of time this may be applicable.

Windows is at liberty to delay updating the actual last write time for a file - as to how long this may be delayed for, check into the Windows API calls dealing with file information.

給妳壹絲溫柔 2024-09-18 01:41:50

根据MSDN

此方法可能返回不准确的
值,因为它使用原生
其值可能不为的函数
运营商持续更新
系统。

如果可能,我会尝试以下操作之一:

  • 在文件名中嵌入日期
  • 将文件版本或日期存储在文件中
    内容。

这样您就不必担心涉及 LastWriteTime 属性的问题。

According to MSDN:

This method may return an inaccurate
value, because it uses native
functions whose values may not be
continuously updated by the operating
system.

If possible, I would try one of the following:

  • Embed the date in the file name
  • Store the file version or date in the file
    contents.

That way you don't have to worry about issues involving the LastWriteTime property.

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