In case you missed Part 1, I briefly summarized 4 different strategies for programmatically determining the current Operating System in C#. The code I posted there utilizes the System.Environment class. It works great and is sufficient for most uses, but sometimes you want need to know everything about the OS, including the edition (i.e. "XP Pro" vs. "XP Home").
For that, Microsoft gives us the lovely Windows Management Instrumentation (WMI) Interface. You can do a lot of really powerful and interesting things with WMI and WQL, but for this purpose, all we care about is getting some information from it.
After reading this, check out Part 2: Using WMI for even more info
I recently needed to programmatically determine the Operating System my software is running on. I was amazed at how difficult it was to find reliable, useful information on this subject. There were bits and pieces here and there, but almost everyone assumed I had some pre-existing knowledge of the process in one manner or another. Hopefully, this little series will save somebody some time in the future.
The thing that makes this so difficult is that there are almost as many different ways to detect the operating system as there are operating systems! It's an unbelievably frustrating experience trying to reinvent this wheel, so I'll try to walk through it here.
There are 4 basic strategies:
- Read and parse information from files in the Windows directory. This is probably the most difficult and least rewarding method. The files are different for each generation of OS and the format is not guaranteed in almost any case.
- Read information from the registry. This method works a little better, but you still have varying locations for the information based on which OS you're dealing with.
- Use Environment variables. Nothing is more reliable. However, it doesn't give you the nitty-gritty detail you might want. For example: There's no way to detect XP Home vs. XP Professional.
- Use WMI to query for information about the OS. This gives you everything you could possibly want all in one shot. Unfortunately, it's not supported in pre-NT versions of Windows (of course, this is becoming less and less of a problem).
I started with number 1. After pulling most of my hair out and wanting to punch my keyboard, I moved on to number 2. That was a dead end too. I could have made it work, but I didn't have the patience. I gave number 3 a try and was very happy... ...until I decided that the specific version of 2000, XP, Vista, and 7 mattered to me. So I finally ended up with a combination of 3 and 4.
For part 1 of this series, I'm going to focus on number 3, a.k.a. The Easy Waytm