我有一种情况,一个用户可能安装了应用程序更新,而该应用程序正在为另一个登录用户运行。
当其他用户重新连接到他们的会话时,调用 toFileVersionInfo.GetVersionInfo(file)
用于检查是否发生了更新并旨在重新启动应用程序。
但是,尽管文件资源管理器中的文件显示了新版本号(文件和产品版本使用相同的版本),但调用GetVersionInfo()
返回的是当前执行版本,而不是磁盘上的版本。
我只设置[assembly: AssemblyVersion("A.B.C.D")]
版本属性,而其他未定义,以便它们默认为该AssemblyVersion
值。
string file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, AppDomain.CurrentDomain.FriendlyName);
/// or
string file = @"C:\Program Files (x86)\Company\Product\product.exe";
FileVersionInfo info = FileVersionInfo.GetVersionInfo(file);
info.FileVersion; /// returns 1.0.0.0
但是,文件资源管理器中的文件版本(和产品版本)详细信息product.exe
表明2.0.0.0
.
我认为执行程序集必须引用某种虚拟化文件或路径,VirtualStore
因为文件系统已更改。
如何可靠地获取目录中显示的实际文件product.exe
版本C:\Program Files (x86)\Company\Product
?
要重现这一点,请使用以下Console
应用程序:
using System;
using System.Diagnostics;
using System.IO;
namespace versiontest
{
class Program
{
static void Main(string[] args)
{
string file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, AppDomain.CurrentDomain.FriendlyName);
Console.WriteLine("Getting FileVersion of {0}: {1}", file, FileVersionInfo.GetVersionInfo(file).FileVersion);
Console.WriteLine("Modify the file and press any key to continue...");
Console.ReadKey();
Console.WriteLine("Getting FileVersion of {0}: {1}", file, FileVersionInfo.GetVersionInfo(file).FileVersion);
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}
}
构建应用程序的两个版本。第一个应该具有程序集版本属性,没有其他版本属性,设置AssemblyInfo.cs
为:
[assembly: AssemblyVersion("1.0.0.0")]
第二个应该将版本设置为
[assembly: AssemblyVersion("2.0.0.0")]
执行版本1.0.0.0
app.exe
,然后在检索到第一个文件版本后,将执行重命名app.exe
为app.exe.bak
并将版本复制2.0.0.0
app.exe
到同一文件夹中。
当我这样做时,这是我的控制台输出:
Getting FileVersion of C:\Users\User\Documents\Company\Source\product\testapp\bin\x86\Debug\app.exe: 1.0.0.0
Modify the file and press any key to continue...
Getting FileVersion of C:\Users\User\Documents\Company\Source\product\testapp\bin\x86\Debug\app.exe: 1.0.0.0
Press any key to exit...
如果您app.exe
立即再次运行,作为版本2.0.0.0
应用程序,控制台输出将显示:
Getting FileVersion of C:\Users\User\Documents\Company\Source\product\testapp\bin\x86\Debug\app.exe: 2.0.0.0