[英]How to recognize Windows OS version?
I am developing .NET 4 WPF application and this application should be able to export data to Excel xls worksheet. 我正在开发.NET 4 WPF应用程序,该应用程序应该能够将数据导出到Excel xls工作表。 However I need to resolve at runtime which version it is running on since there are still computers with Windows XP and Office 97/2000 on them.
但是我需要在运行时确定它正在运行哪个版本,因为仍然有配备Windows XP和Office 97/2000的计算机。
I wasn't able to export xls trough my application on Windows XP, Office 97/2000 because we don't own Office Interop libraries, versions 7, 8, or 9. 我无法通过Windows XP,Office 97/2000上的应用程序导出xls,因为我们不拥有版本7、8或9的Office Interop库。
Therefore I will export to CSV on Windows XP and xls on Windows 7 and Windows 10. 因此,我将在Windows XP上导出为CSV,在Windows 7和Windows 10上导出为xls。
I tried this code snippet but I am unsure if it is enough info to recognize if it is Win XP, 7 or 10. 我尝试了此代码段,但不确定是否足以识别它是Win XP,7还是10。
public static string getOSVersion()
{
return Environment.OSVersion.ToString() + ", " + Environment.Version;
}
I am getting this on Windows 10: 我在Windows 10上得到这个:
Microsoft Windows NT 6.2.9200.0, 4.0.30319.42000
Is this enough information or is there a better way to recognize Windows OS Name? 这是足够的信息,还是有更好的方法来识别Windows OS名称?
System.Environment.Version
will retrieve the Version of the .NET Framework used to execute your code, so this will not help you. System.Environment.Version
将检索用于执行代码的.NET Framework的版本,因此这对您没有帮助。
The System.Environment.OSVersion
object (OperatingSystem class) is indeed the correct way to get the Windows version. 实际上,
System.Environment.OSVersion
对象(OperatingSystem类)是获取Windows版本的正确方法。 You should however do something like this: 但是,您应该执行以下操作:
public static bool IsWindowsCurrentGen()
{
if (Environment.OSVersion.Version.Major >= 6)
return true;
else
return false;
}
This will return true
for everything above (including) Windows Vista. 对于Windows Vista以上的所有版本(包括Windows Vista),都将返回
true
。
For a "full" reference table see this StackOverflow question . 有关“完整”参考表,请参见此StackOverflow问题 。 Note, that it would be better from my point of view, to detect the installed Excel version rather than the OS version.
请注意,从我的角度来看,最好检测安装的Excel版本而不是OS版本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.