How can I detect which version of Windows is runni

2019-07-12 19:52发布

I have an application which runs as a Word add-in. (VSTO).

It runs on many PCs all over the world. In certain cases I need to trouble shoot problems (obviously remotely) and have a reporting mechanism which tells me which Windows OS is running - I use Environment.OSVersion in .NET. At least it did until Windows 10.

There is an article on MSDN (Targeting your application for Windows) about creating an application manifest which will enable the correct version to be returned.

But my application is a DLL, not an EXE and so won't really accommodate the XML code referred to in that article.

Is there no way to just ask Windows, "What version are you? Really, I won't cry if you admit to the real version".

Or an entry in the registry or something?

标签: c# windows
3条回答
Lonely孤独者°
2楼-- · 2019-07-12 20:37

You can try GetVersionEx Win32 API.

https://msdn.microsoft.com/en-us/library/windows/desktop/ms724451(v=vs.85).aspx

Note that Windows 10 in fact is version 6.4. Ver 6.0 is Vista, 6.1 - 7, 6.2 - 8.

查看更多
女痞
3楼-- · 2019-07-12 20:39

WMI is the best way to do this kind of stuff You can use this to retrieve OS informations :

ManagementObjectSearcher objMOS = 
       new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM  Win32_OperatingSystem");

OS.Name = ((string)objMOS["Caption"]).Trim();
OS.Version = (string)objMOS["Version"];
OS.MaxProcessCount = (uint)objMOS["MaxNumberOfProcesses"];
OS.MaxProcessRAM = (ulong)objMOS["MaxProcessMemorySize"];
OS.Architecture = (string)objMOS["OSArchitecture"];
OS.SerialNumber = (string)objMOS["SerialNumber"];
OS.Build = ((string)objMOS["BuildNumber"]).ToUint();

This can fetch the OS details for you.

查看更多
Fickle 薄情
4楼-- · 2019-07-12 20:45
private String GetOSVersion()
{
    var wmiEnum = new ManagementObjectSearcher("root\\CIMV2", "SELECT Version FROM  Win32_OperatingSystem").Get().GetEnumerator();
    wmiEnum.MoveNext();
    return wmiEnum.Current.Properties["Version"].Value as String;
}

Returns 6.1.7601 on my W7 system and 10.0.14393 on my Server 2016 system.

No need to add a target manifest.

查看更多
登录 后发表回答