C# 如何以编程方式获取UWP(应用程序)目标appversion?
我已经创建了UWP应用程序。我使用了一些仅在UWP的最新版本(1904117763)中可用的API。我想在以前和最新版本中运行。因此,如果我以编程方式获取应用程序目标版本,我将检查使应用程序与每个版本兼容的条件。是否可以获取应用程序目标版本。您可以使用powershell命令从文件元数据获取版本(您必须确保您的产品在其中具有有效值) 然后您可以使用以下逻辑: on path参数将路径发送到更新了文件元数据的应用程序可执行文件(或dll)字段C# 如何以编程方式获取UWP(应用程序)目标appversion?,c#,uwp,C#,Uwp,我已经创建了UWP应用程序。我使用了一些仅在UWP的最新版本(1904117763)中可用的API。我想在以前和最新版本中运行。因此,如果我以编程方式获取应用程序目标版本,我将检查使应用程序与每个版本兼容的条件。是否可以获取应用程序目标版本。您可以使用powershell命令从文件元数据获取版本(您必须确保您的产品在其中具有有效值) 然后您可以使用以下逻辑: on path参数将路径发送到更新了文件元数据的应用程序可执行文件(或dll)字段 string GetVersion(string pa
string GetVersion(string path)
{
var command = $"(Get-Command \"{path}\").FileVersionInfo.ProductVersion";
var processStartInfo = new ProcessStartInfo
{
FileName = "cli",
Arguments = $"/c powershell {command}",
WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden,
RedirectStandardOutput = true,
UseShellExecute = false
};
var p = System.Diagnostics.Process.Start(proc);
p.WaitForExit();
var fileVersion = p.StandardOutput.ReadToEnd();
return fileVersion;
}
此解决方案适用于本地和远程目标
如果您仅在本地需要它,您只需使用FileVersionInfo类并将其从FileVersion属性中取出即可
如MSDN示例所示:
public static void Main(string[] args)
{
// Get the file version for the notepad.
// Use either of the two following commands.
FileVersionInfo.GetVersionInfo(Path.Combine(Environment.SystemDirectory, "Notepad.exe"));
FileVersionInfo myFileVersionInfo = FileVersionInfo.GetVersionInfo(Environment.SystemDirectory + "\\Notepad.exe");
// Print the file name and version number.
Console.WriteLine("File: " + myFileVersionInfo.FileDescription + '\n' +
"Version number: " + myFileVersionInfo.FileVersion);
}
您可以在此处阅读更多内容:
您可以使用powershell命令从文件元数据获取版本(您必须确保您的产品在其中具有有效值) 然后您可以使用以下逻辑: on path参数将路径发送到更新了文件元数据的应用程序可执行文件(或dll)字段
string GetVersion(string path)
{
var command = $"(Get-Command \"{path}\").FileVersionInfo.ProductVersion";
var processStartInfo = new ProcessStartInfo
{
FileName = "cli",
Arguments = $"/c powershell {command}",
WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden,
RedirectStandardOutput = true,
UseShellExecute = false
};
var p = System.Diagnostics.Process.Start(proc);
p.WaitForExit();
var fileVersion = p.StandardOutput.ReadToEnd();
return fileVersion;
}
此解决方案适用于本地和远程目标
如果您仅在本地需要它,您只需使用FileVersionInfo类并将其从FileVersion属性中取出即可
如MSDN示例所示:
public static void Main(string[] args)
{
// Get the file version for the notepad.
// Use either of the two following commands.
FileVersionInfo.GetVersionInfo(Path.Combine(Environment.SystemDirectory, "Notepad.exe"));
FileVersionInfo myFileVersionInfo = FileVersionInfo.GetVersionInfo(Environment.SystemDirectory + "\\Notepad.exe");
// Print the file name and version number.
Console.WriteLine("File: " + myFileVersionInfo.FileDescription + '\n' +
"Version number: " + myFileVersionInfo.FileVersion);
}
您可以在此处阅读更多内容:
您可以在应用程序安装文件夹中获取AppxManifest.xml文件。之后,可以使用加载xml文件。在AppxManifest.xml中,您可以在包->依赖项下找到一个名为TargetDeviceFamily的节点,它包含一个名为MaxVersionTested的值,该值是目标版本的值 以下是您可以检查的代码段:
var file = await Package.Current.InstalledLocation.GetFileAsync("AppxManifest.xml");
var xmlDocument = await XmlDocument.LoadFromFileAsync(file);
var VersionText = xmlDocument.ChildNodes.LastOrDefault().ChildNodes.Where(p => p.NodeName == "Dependencies").FirstOrDefault().ChildNodes.Where(t => t.NodeName == "TargetDeviceFamily").FirstOrDefault().Attributes.GetNamedItem("MaxVersionTested").InnerText;
MyTextBlock.Text = VersionText;
您可以在app安装文件夹中获取AppxManifest.xml文件。之后,可以使用加载xml文件。在AppxManifest.xml中,您可以在包->依赖项下找到一个名为TargetDeviceFamily的节点,它包含一个名为MaxVersionTested的值,该值是目标版本的值 以下是您可以检查的代码段:
var file = await Package.Current.InstalledLocation.GetFileAsync("AppxManifest.xml");
var xmlDocument = await XmlDocument.LoadFromFileAsync(file);
var VersionText = xmlDocument.ChildNodes.LastOrDefault().ChildNodes.Where(p => p.NodeName == "Dependencies").FirstOrDefault().ChildNodes.Where(t => t.NodeName == "TargetDeviceFamily").FirstOrDefault().Attributes.GetNamedItem("MaxVersionTested").InnerText;
MyTextBlock.Text = VersionText;
谢谢你的代码,这是获得目标版本的唯一方法。编辑并添加了另一种方法。嗨@michael Gabbay,我听过一些Api获得目标版本,如AnalyticsVersionInfo。我已经试过了,但没有给出正确的版本。有没有这样的api来获取目标版本。试试看这个问题我需要获取运行UWPAP的targetsdkversion。例如,我将我的目标版本设置为Windows 10,版本1809(10.0 Build 17763),我需要以编程方式使用此目标版本。感谢代码,这是获得目标版本的唯一方法。编辑并添加了另一种方法。嗨@michael Gabbay,我听过一些Api获得目标版本,如AnalyticsVersionInfo。我已经试过了,但没有给出正确的版本。有没有这样的api来获取目标版本。试试看这个问题我需要获取运行UWPAP的targetsdkversion。例如,Im将我的目标版本设置为Windows 10,版本1809(10.0 Build 17763),我需要以编程方式使用此目标版本。