0

I am developing .NET 4 WPF application and this application should be able to export data to Excel xls worksheet. 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.

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.

Therefore I will export to CSV on Windows XP and xls on Windows 7 and Windows 10.

I tried this code snippet but I am unsure if it is enough info to recognize if it is Win XP, 7 or 10.

 public static string getOSVersion()
 {
      return Environment.OSVersion.ToString() + ", " + Environment.Version;
 }

I am getting this on 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?

Vlad
  • 2,739
  • 7
  • 49
  • 100
  • 1
    I think [this](https://stackoverflow.com/a/32415824/7405620) answer can help you . – majid zareei Feb 22 '18 at 10:03
  • You can export to Excel without having the Excel installed (no Interop needed). See e.g. https://github.com/ClosedXML/ClosedXML – xmojmr Feb 22 '18 at 10:50

1 Answers1

2

System.Environment.Version will retrieve the Version of the .NET Framework used to execute your code, so this will not help you.

The System.Environment.OSVersion object (OperatingSystem class) is indeed the correct way to get the Windows version. 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.

For a "full" reference table see this StackOverflow question. Note, that it would be better from my point of view, to detect the installed Excel version rather than the OS version.

Daniel Fuchs
  • 306
  • 2
  • 8