【问题标题】:Getting the version number in code .Net Core 3 WPF Sideload获取代码中的版本号.Net Core 3 WPF Sideload
【发布时间】:2019-11-01 19:04:04
【问题描述】:

我能够使用 .NET Core 3.0 WPF 和旁加载(Windows Store 应用程序包,但来自本地 UNC 路径)构建、部署和安装我的项目。这一切似乎都运行良好,但我找不到通过代码获取当前版本号的方法,以便在应用运行时向用户显示。

我已经尝试过使用反射和 XML 在 ClickOnce 中一直使用的方法。 我还尝试使用我在其他地方看到的 UWP 方法,但似乎无法在 WPF/.NET Core 3 中使用 Windows.ApplicationModel

这种方式在带有 ClickOnce 的 .NET Framework 中对我有用

XmlDocument xmlDoc = new System.Xml.XmlDocument();
Assembly asmCurrent = System.Reflection.Assembly.GetExecutingAssembly();
string executePath = new Uri(asmCurrent.GetName().CodeBase).LocalPath;
xmlDoc.Load(executePath + ".manifest");
string retval = string.Empty;
if (xmlDoc.HasChildNodes)
{
     retval = xmlDoc.ChildNodes[1].ChildNodes[0].Attributes.GetNamedItem("version").Value.ToString();
}
return new Version(retval).ToString();

这看起来就像人们使用 UWP 和 Windows 应用商店所做的那样

Package package = Windows.ApplicationModel.Package.Current;
PackageId packageId = package.Id;
PackageVersion version = packageId.Version;

我也尝试过使用 Reflection 执行此操作,但它始终返回 1.0.0.0,即使在部署时也是如此。

System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
return fvi.FileVersion;

【问题讨论】:

    标签: c# .net wpf windows .net-core


    【解决方案1】:

    您可以简单地加载 MSIX 的清单:

    private Version GetMsixPackageVersion()
        {
            var assembly = System.Reflection.Assembly.GetExecutingAssembly();
            var manifestPath = assembly.Location.Replace(assembly.ManifestModule.Name, "") + "\\AppxManifest.xml";
            if (File.Exists(manifestPath))
            {
                var xDoc = XDocument.Load(manifestPath);
                return new Version(xDoc.Descendants().First(e => e.Name.LocalName == "Identity").Attributes()
                    .First(a => a.Name.LocalName == "Version").Value);
            }
    
            return new Version(0,0,0,0);
        }
    

    请注意,它在开发过程中不可用,尽管我只是将清单复制到调试文件夹以进行测试。

    【讨论】:

    • 我在执行文件夹中没有看到 AppManifest.xml 文件...
    • AppxManifest.xml 是通过构建您的 msix 项目生成的。检查 bin\(solution platform)\(solution config) 文件夹。
    猜你喜欢
    • 2021-05-11
    • 2020-06-02
    • 1970-01-01
    • 2020-08-19
    • 2021-09-28
    • 2018-07-12
    • 1970-01-01
    • 2014-06-21
    • 2018-08-28
    相关资源
    最近更新 更多