2017-07-29 19 views
0

我有一個我爲Windows創建的應用程序:PC /平板電腦8.1,移動/電話8.1和UWP 10.如何確定Windows應用程序的操作系統(OS)(UWP/PC/Tablet 8.1/Mobile 8.x)?

這是一個使用C#的WinRT應用程序。

要在應用中放置廣告條幅,需要爲每個操作系統製作一個單獨的廣告單元ID。

是否有方法可以確定當前正在使用哪個操作系統?

它可以檢查正在使用的設備利用代碼:

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

但如何知道,如果操作系統是Windows 8.1或Windows 10?

UPDATE:

我曾經碰到過一個有趣的文章有關獲取操作系統版本爲C#/ XAML:

Windows Store Apps: Get OS Version, beginners tutorials (C#-XAML)

它使用System.Type.GetType檢查Windows.System。 Profile.AnalyticsVersionInfo返回null。

我修改並測試了代碼,它似乎可以在Visual Studio模擬器和模擬器中工作。我無法測試Windows 8.1計算機,因爲我使用的是Windows 10計算機,但對於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; 

UPDATE:

一行代碼:

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

回答

0

試試這個,我發現它在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()); 
    } 
} 
+0

此.NET代碼在我的WinRT應用程序中不起作用。另外,頁面上的第一個標註爲「重要」的註釋表示:「我們不建議您檢索此屬性的值來確定操作系統版本」,這正是我想要做的。 – theMaxx

0

你可以只檢查一類是隻在系統中存在添加在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; 
} 
+0

這似乎有點冒險和複雜。沒有簡單的方法嗎? – theMaxx

+0

爲什麼它有風險?您只需檢查系統上是否存在某種類型的激活工廠。您的Type.GetType()方法更適用於相同的最終結果。而且這也快得多。 – Sunius

+0

單線程怎麼樣? var isWindows10 = Type.GetType(「Windows.System.Profile.AnalyticsVersionInfo,Windows,ContentType = WindowsRuntime」)!= null?真假; 它可以節省大約十幾行代碼,不需要導入文件或運行激活工廠。 我仍然不確定,但如果這實際上可能是所有情況下的有效解決方案。 – theMaxx

相關問題