【问题标题】:How to Determine the Operating System (OS) for a Windows App (UWP / PC/Tablet 8.1 / Mobile 8.x)?如何确定 Windows 应用程序(UWP/PC/Tablet 8.1/Mobile 8.x)的操作系统(OS)?
【发布时间】:2018-01-05 17:40:53
【问题描述】:

我有一个为 Windows 创建的应用:PC/Tablet 8.1、Mobile/Phone 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 Store Apps: Get OS Version, beginners tutorials (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;

【问题讨论】:

    标签: c# windows-runtime operating-system


    【解决方案1】:

    试试这个,我在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());
        }
    }
    

    【讨论】:

    • 此 .NET 代码在我的 WinRT 应用程序中不起作用。此外,页面上标记为“重要”的第一条评论说:“我们不建议您检索此属性的值来确定操作系统版本”,这正是我想要做的。
    【解决方案2】:

    您可以检查系统上是否存在仅在 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;
    }
    

    【讨论】:

    • 这似乎有点冒险和复杂。没有更简单的方法吗?
    • 为什么有风险?您只是在检查系统上是否存在某种类型的激活工厂。您的 Type.GetType() 方法为相同的最终结果工作更多。这也快得多。
    • 单线怎么样? var isWindows10 = Type.GetType("Windows.System.Profile.AnalyticsVersionInfo, Windows, ContentType=WindowsRuntime") != null ?真假;它节省了大约十几行代码,并且不需要导入文件或运行激活工厂。我仍然不确定这是否真的是适用于所有场景的有效解决方案。
    【解决方案3】:
    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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-03
      • 1970-01-01
      相关资源
      最近更新 更多