我有一个带有FileSystemWatcher的程序,它监视自己被外部程序更新到新版本(这涉及到重命名当前的可执行文件并复制一个新的可执行文件)。
问题是,当它正在查看的文件位于Program Files目录中时,FileVersionInfo.GetVersionInfo()不会获得新的版本信息,它会返回与第一次相同的信息。因此,如果它从1.1更新到1.2,它会说“从1.1升级到1.1”,而不是“从1.1升级到1.2”。它在debug目录中工作正常,但在Program Files下,它不会获得正确的值。
以下是它所做的事情的本质,没有所有的异常处理、处置、日志记录和线程调用等等:
string oldVersion;
long oldSize;
DateTime oldLastModified;
FileSystemWatcher fs;
string fullpath;
public void Watch()
{
fullpath = Assembly.GetEntryAssembly().Location;
oldVersion = FileVersionInfo.GetVersionInfo(fullpath).ProductVersion;
var fi = new FileInfo(fullpath);
oldSize = fi.Length;
oldLastModified = fi.LastWriteTime;
fs = new FileSystemWatcher(
Path.GetDirectoryName(fullpath), Path.GetFileName(file));
fs.Changed += FileSystemEventHandler;
fs.Created += FileSystemEventHandler;
fs.EnableRaisingEvents = true;
}
void FileSystemEventHandler(object sender, FileSystemEventArgs e)
{
if (string.Equals(e.FullPath, fullpath, StringComparison.OrdinalIgnoreCase))
{
var fi = new FileInfo(fullpath);
if (fi.Length != oldSize
|| fi.LastWriteTime != oldLastModified)
{
var newversion = FileVersionInfo.GetVersionInfo(fullpath).ProductVersion;
NotifyUser(oldVersion, newversion);
}
}
}如何刷新GetVersionInfo()以查看新版本?还有什么是我应该打给他的吗?
发布于 2013-05-24 04:09:08
我正在回答我自己的问题,因为似乎没有太多的兴趣。如果有人有更好的答案,我会接受的。
据我所知,没有办法让它刷新。相反,我解决了这个问题:
return AssemblyName.GetAssemblyName(fullpath).Version.ToString();与确保它只被调用一次的代码相结合,它似乎工作得很好。
https://stackoverflow.com/questions/16656996
复制相似问题