WIN8.1开发如何获得当前的操作系统呢?

←****才 UID.360018
2015-10-23 发表

System.Environment类已经无法获得操作系统的版本是win8.1还是win10,请问有什么办法没?

敬告:
为防止不可控的内容风险,本站已关闭新用户注册,新贴的发表及评论;
你现在看到的内容只是互联网用户曾经发表的言论快照,仅用于老用户留存纪念,且仅与科技行业相关,全部内容不代表本站观点及立场;
本站重新开放前已针对包括用户隐私、版权保护、信息安全、国家政策在内的各种互联网法律法规要求,执行了隐患内容的自查、屏蔽和删除;
本站目前所属个人主体,未有任何盈利安排与计划,且与原WFUN.COM所属公司不存在任何关联关系;
如果本帖内容或者相关资源侵犯到您的合法权益,或者您认为存在问题,那么请您务必点此举报或投诉!
全部回复:
qiqiminmin UID.638527
2015-10-24 回复

本帖最后由 qiqiminmin 于 2015-10-24 10:15 编辑

利用windows api sets


[mw_shl_code=csharp,true]using System;
using System.Runtime.InteropServices;

public sealed partial class MainPage : Page
{
[DllImport("api-ms-win-core-sysinfo-l1-2-1.dll", CharSet = CharSet.Ansi)]
private static extern long GetVersionExA([In, Out] OSVersionInfo info );

[StructLayout(LayoutKind.Sequential)]
public class OSVersionInfo
{
public int dwOSVersionInfoSize;
public int dwMajorVersion;
public int dwMinorVersion;
public int dwBuildNumber;
public int dwPlatformId;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public String szCSDVersion;
}


public MainPage()
{
this.InitializeComponent();

this.Loaded += MainPage_Loaded;
}

private void MainPage_Loaded(object sender, RoutedEventArgs e)
{

OSVersionInfo osvi = new OSVersionInfo();
osvi.dwOSVersionInfoSize = Marshal.SizeOf(osvi);
GetVersionExA(osvi);


// throw new NotImplementedException();
}
}[/mw_shl_code]


如果懂这个的就知道为什么windows 8后面是10了。


windows 10, windows 8.1 均支持 api-ms-win-core-sysinfo-l1-2-1.dll 的api,所以可以放心使用。
windows 95, dwMajorVersion=9
windows 8, dwMajorVersion =8
windows 10, dwMajorVersion = 10

tmp00000 UID.995403
2015-10-24 回复

本帖最后由 tmp00000 于 2015-10-24 11:56 编辑

Imports Windows.Devices.Enumeration.Pnp
Module SystemVersion
Const DeviceDriverVersionKey = "{A8B865DD-2E3D-4094-AD97-E593A70C75D6},3"
Friend Async Function GetWindowsVersionAsync() As Task(Of String)
Dim hal As PnpObject = Nothing
Dim actualProperties = {DeviceDriverVersionKey, "{A45C254E-DF1C-4EFD-8020-67D146A850E0},10"}
Dim rootDevices = Await PnpObject.FindAllAsync(PnpObjectType.Device, actualProperties, "System.Devices.ContainerId:=""{00000000-0000-0000-FFFF-FFFFFFFFFFFF}""")
For Each rootDevice In From d In rootDevices Where d.Properties IsNot Nothing AndAlso d.Properties.Any
Dim lastProperty = rootDevice.Properties.Last.Value
If lastProperty IsNot Nothing Then
If lastProperty.ToString = "4d36e966-e325-11ce-bfc1-08002be10318" Then
hal = rootDevice
Exit For
End If
End If
Next
Return If((hal?.Properties.ContainsKey(DeviceDriverVersionKey)).GetValueOrDefault, hal.Properties(DeviceDriverVersionKey).ToString, String.Empty)
End Function
End Module
这个不需要pinvoke也能拿到完整的版本号

qiqiminmin UID.638527
2015-10-25 回复

本帖最后由 qiqiminmin 于 2015-10-27 23:00 编辑

Quote***链接停止解析***
Imports Windows.Devices.Enumeration.Pnp
Module SystemVersion
Const DeviceDriverVersionKey = "{A8 ...


嗯,您这个也不错,改成C#

[mw_shl_code=csharp,true]// Copyright (c) Attack Pattern LLC. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at ***链接停止解析***

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Windows.Devices.Enumeration.Pnp;

namespace CSharpAnalytics.SystemInfo
{
/// <summary>
/// Obtain system information not conveniently exposed by WinRT APIs.
/// </summary>
/// <remarks>
/// Microsoft doesn't really want you getting this information and makes it difficult.
/// The techniques used here are not bullet proof but are good enough for analytics.
/// Do not use these methods or techniques for anything more important than that.
/// (Note that this class was also published as SystemInfoEstimate on our blog)
/// </remarks>
public static class WindowsStoreSystemInfo
{
private const string ModelNameKey = "System.Devices.ModelName";
private const string ManufacturerKey = "System.Devices.Manufacturer";
private const string DisplayPrimaryCategoryKey = "{78C34FC8-104A-4ACA-9EA4-524D52996E57},97";
private const string DeviceDriverKey = "{A8B865DD-2E3D-4094-AD97-E593A70C75D6}";
private const string DeviceDriverVersionKey = DeviceDriverKey + ",3";
private const string DeviceDriverProviderKey = DeviceDriverKey + ",9";
private const string RootContainer = "{00000000-0000-0000-FFFF-FFFFFFFFFFFF}";
private const string RootContainerQuery = "System.Devices.ContainerId:=\"" + RootContainer + "\"";

/// <summary>
/// Build a system user agent string that contains the Windows version number
/// and CPU architecture.
/// </summary>
/// <returns>String containing formatted system parts of the user agent.</returns>
public static async Task<string> GetSystemUserAgentAsync()
{
try
{
var parts = new[] {
"Windows NT " + await GetWindowsVersionAsync(),
FormatForUserAgent(GetProcessorArchitecture())
};

return "(" + String.Join("; ", parts.Where(e => !String.IsNullOrEmpty(e))) + ")";
}
catch
{
return "";
}
}

/// <summary>
/// Format a ProcessorArchitecture as it would be expected in a user agent of a browser.
/// </summary>
/// <returns>String containing the format processor architecture.</returns>
static string FormatForUserAgent(ProcessorArchitecture architecture)
{
switch (architecture)
{
case ProcessorArchitecture.AMD64:
return "x64";
case ProcessorArchitecture.ARM:
return "ARM";
default:
return "";
}
}

/// <summary>
/// Get the processor architecture of this computer.
/// </summary>
/// <returns>The processor architecture of this computer.</returns>
public static ProcessorArchitecture GetProcessorArchitecture()
{
try
{
var sysInfo = new _SYSTEM_INFO();
GetNativeSystemInfo(ref sysInfo);

return Enum.IsDefined(typeof(ProcessorArchitecture), sysInfo.wProcessorArchitecture)
? (ProcessorArchitecture)sysInfo.wProcessorArchitecture
: ProcessorArchitecture.UNKNOWN;
}
catch
{
}

return ProcessorArchitecture.UNKNOWN;
}

/// <summary>
/// Get the name of the manufacturer of this computer.
/// </summary>
/// <example>Microsoft Corporation</example>
/// <returns>The name of the manufacturer of this computer.</returns>
public static async Task<string> GetDeviceManufacturerAsync()
{
var rootContainer = await PnpObject.CreateFromIdAsync(PnpObjectType.DeviceContainer, RootContainer, new[] { ManufacturerKey });
return (string)rootContainer.Properties[ManufacturerKey];
}

/// <summary>
/// Get the name of the model of this computer.
/// </summary>
/// <example>Surface with Windows 8</example>
/// <returns>The name of the model of this computer.</returns>
public static async Task<string> GetDeviceModelAsync()
{
var rootContainer = await PnpObject.CreateFromIdAsync(PnpObjectType.DeviceContainer, RootContainer, new[] { ModelNameKey });
return (string)rootContainer.Properties[ModelNameKey];
}

/// <summary>
/// Get the device category this computer belongs to.
/// </summary>
/// <example>Computer.Desktop, Computer.Tablet</example>
/// <returns>The category of this device.</returns>
public static async Task<string> GetDeviceCategoryAsync()
{
var rootContainer = await PnpObject.CreateFromIdAsync(PnpObjectType.DeviceContainer, RootContainer, new[] { DisplayPrimaryCategoryKey });
return (string)rootContainer.Properties[DisplayPrimaryCategoryKey];
}

/// <summary>
/// Get the version of Windows for this computer.
/// </summary>
/// <example>6.2</example>
/// <returns>Version number of Windows running on this computer.</returns>
public static async Task<string> GetWindowsVersionAsync()
{
// There is no good place to get this so we're going to use the most popular
// Microsoft driver version number from the device tree.
var requestedProperties = new[] { DeviceDriverVersionKey, DeviceDriverProviderKey };

var microsoftVersionedDevices = (await PnpObject.FindAllAsync(PnpObjectType.Device, requestedProperties, RootContainerQuery))
.Select(d => new { Provider = (string)d.Properties.GetValueOrDefault(DeviceDriverProviderKey),
Version = (string)d.Properties.GetValueOrDefault(DeviceDriverVersionKey) })
.Where(d => d.Provider == "Microsoft" && d.Version != null)
.ToList();

var versionNumbers = microsoftVersionedDevices
.GroupBy(d => d.Version.Substring(0, d.Version.IndexOf('.', d.Version.IndexOf('.') + 1)))
.OrderByDescending(d => d.Count())
.ToList();

var confidence = (versionNumbers[0].Count() * 100 / microsoftVersionedDevices.Count);
return versionNumbers.Count > 0 ? versionNumbers[0].Key : "";
}

static TValue GetValueOrDefault<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dictionary, TKey key)
{
TValue value;
return dictionary.TryGetValue(key, out value) ? value : default(TValue);
}

[DllImport("api-ms-win-core-sysinfo-l1-2-1.dll")]
static extern void GetNativeSystemInfo(ref _SYSTEM_INFO lpSystemInfo);

[StructLayout(LayoutKind.Sequential)]
struct _SYSTEM_INFO
{
public ushort wProcessorArchitecture;
public ushort wReserved;
public uint dwPageSize;
public IntPtr lpMinimumApplicationAddress;
public IntPtr lpMaximumApplicationAddress;
public UIntPtr dwActiveProcessorMask;
public uint dwNumberOfProcessors;
public uint dwProcessorType;
public uint dwAllocationGranularity;
public ushort wProcessorLevel;
public ushort wProcessorRevision;
};
}

public enum ProcessorArchitecture : ushort
{
INTEL = 0,
MIPS = 1,
ALPHA = 2,
PPC = 3,
SHX = 4,
ARM = 5,
IA64 = 6,
ALPHA64 = 7,
MSIL = 8,
AMD64 = 9,
IA32_ON_WIN64 = 10,
UNKNOWN = 0xFFFF
}
}[/mw_shl_code]


←****才 UID.360018
2015-10-26 回复

大神,果然厉害!!

本站使用Golang构建,点击此处申请开源鄂ICP备18029942号-4联系站长投诉/举报