In a .NET 2.0 C# application I use the following code to detect the operating system platform:
string os_platform = System.Environment.OSVersion.Platform.ToString();
This returns "Win32NT". The problem is that it returns "Win32NT" even when running on Windows Vista 64-bit.
Is there any other method to know the correct platform (32 or 64 bit)?
Note that it should also detect 64 bit when run as a 32 bit application on Windows 64 bit.
OSInfo.Bits
@foobar: You are right, it is too easy ;)
In 99% of the cases, developers with weak system administrator backgrounds ultimately fail to realize the power Microsoft has always provided for anyone to enumerate Windows.
System administrators will always write better and simpler code when it comes to such a point.
Nevertheless, one thing to note, build configuration must be AnyCPU for this environment variable to return the correct values on the correct systems:
This will return "X86" on 32-bit Windows, and "AMD64" on 64-bit Windows.
All fine, but this should also work from
env
:..
Too easy, maybe ;-)
I use:
This gets the path where your application is launched in case you have it installed in various places on the computer. Also, you could just do the general
C:\
path since 99.9% of computers out there have Windows installed inC:\
.Microsoft has put a code sample for this:
http://1code.codeplex.com/SourceControl/changeset/view/39074#842775
It looks like this:
There is a WMI version available as well (for testing remote machines).
I need to do this, but I also need to be able as an admin do it remotely, either case this seems to work quite nicely for me: