我的应用程序在启动时检查共享网络驱动器上是否有较新版本的文件。如果网络共享上的应用程序较新,则会将其复制到本地应用程序目录并覆盖旧的。我当前执行此操作的代码如下所示:
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
.
发布评论
评论(2)
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.
根据MSDN:
如果可能,我会尝试以下操作之一:
内容。
这样您就不必担心涉及
LastWriteTime
属性的问题。According to MSDN:
If possible, I would try one of the following:
contents.
That way you don't have to worry about issues involving the
LastWriteTime
property.