如何确定Windows应用程序(UWP / PC / Tablet 8.1 / Mobile 8.x)的操作系统(OS)



我有一个为Windows创建的应用程序:PC/平板电脑8.1,手机8.1和UWP 10。

它是一个使用 C# 的 WinRT 应用。

要在应用中放置广告横幅,需要为每个操作系统制作单独的广告单元 ID。

有没有办法确定当前正在使用哪个操作系统?

可以使用代码检查正在使用的设备:

#if WINDOWS_PHONE_APP
isWindowsPhoneApp = true;
#else
isWindowsPhoneApp = false;
#endif

但是如何知道操作系统是Windows 8.1还是Windows 10?

更新:

我遇到了一篇关于获取 C#/XAML 操作系统版本的有趣文章:

Windows 应用商店应用:获取操作系统版本,初学者教程 (C#-XAML(

它使用 System.Type.GetType 来检查 Windows.System.Profile.AnalyticsVersionInfo 是否返回 null。

我修改并测试了代码,它似乎可以在Visual Studio模拟器和模拟器中工作。 由于我使用的是Windows 10计算机,因此我无法测试Windows 8.1计算机,但对于Windows Phone 8.1和Windows 10 Mobile,它是准确的。 我还没有在实际的手机设备上测试过它。

因此,检查仅在Windows 10中可用的AnalyticsVersionInfo类型似乎将返回true或false,具体取决于操作系统。

那么,是否建议在发布版本中使用以下代码?

var analyticsVersionInfoType = Type.GetType("Windows.System.Profile.AnalyticsVersionInfo, Windows, ContentType=WindowsRuntime");
var isWindows10 = analyticsVersionInfoType != null;
displayTextBlock.Text = "Is Windows 10: " + isWindows10;

更新:

单行:

var isWindows10 = Type.GetType("Windows.System.Profile.AnalyticsVersionInfo, Windows, ContentType=WindowsRuntime") != null;

试试这个,我在 https://msdn.microsoft.com/en-us/library/system.environment.osversion(v=vs.110(中找到了它.aspx

using System;
class Sample 
{
public static void Main() 
{
Console.WriteLine();
Console.WriteLine("OSVersion: {0}", Environment.OSVersion.ToString());
}
}

您可以检查系统上是否存在仅在Windows 10中添加的类。

[DllImport("API-MS-WIN-CORE-WINRT-L1-1-0.DLL")]
private static extern int/* HRESULT */ RoGetActivationFactory([MarshalAs(UnmanagedType.HString)]string typeName, [MarshalAs(UnmanagedType.LPStruct)] Guid factoryIID, out IntPtr factory);
static bool IsWindows10()
{
IntPtr factory;
var IID_IActivationFactory = new Guid(0x00000035, 0x0000, 0x0000, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46);
var hr = RoGetActivationFactory("Windows.ApplicationModel.ExtendedExecution.ExtendedExecutionSession", IID_IActivationFactory, out factory);
if (hr < 0)
return false;
Marshal.Release(factory);
return true;
}
switch (Environment.OSVersion.Platform)
{
case PlatformID.Unix:
//do android stuff
break;
case PlatformID.MacOSX:
//do apple stuff
break;
case PlatformID.Win32NT:
//do windows NT stuff
break;
case PlatformID.Win32Windows:
//do windows 95 and 98 stuff
break;
}

最新更新