C # Get Windows operating system version Information Instance Tutorial

Source: Internet
Author: User
Tags pack

Using C #, a simple library is written to achieve the effect. The usage might be as follows:

StringBuilder sb = new StringBuilder (String.Empty);
Sb. Appendline ("Operation System Information");
Sb. Appendline ("——————————————");
Sb. Appendline (String.Format ("Name = {0}", Osversioninfo.name));
Sb. Appendline (String.Format ("Edition = {0}", osversioninfo.edition));
if (osversioninfo.servicepack!=string. Empty)
Sb. Appendline (String.Format ("Service Pack = {0}", Osversioninfo.servicepack));
Else
Sb. Appendline ("Service Pack = None");
Sb. Appendline (String.Format ("Version = {0}", osversioninfo.versionstring));
Sb. Appendline (String.Format ("processorbits = {0}", osversioninfo.processorbits));
Sb. Appendline (String.Format ("osbits = {0}", osversioninfo.osbits));
Sb. Appendline (String.Format ("programbits = {0}", osversioninfo.programbits));

TextBox1.Text = sb. ToString ();

Compare some of the few drawbacks of the anecdotal:
Overall. The biggest problem is the inability to properly detect whether your operating system is 32 digits or 64 digits. Several methods are roughly as follows:

1. Use the size of the IntPtr pointer

The most critical piece of code is:

return intptr.size * 8;


But in fact, this does not return the number of bits of the operating system, it returns the number of bits of the running program, and if you run the program in 32-bit mode on 64-bit Windows, it returns 32.

2. Using processor_architecture environment Variables

String pa = environment.getenvironmentvariable ("Processor_architecture");
Return (String.IsNullOrEmpty (PA) | | String.Compare (PA, 0,
"x86", 0, 3, true) = = 0)? 32:64);


This is purely misleading, because it is the same as in 1. Instead of returning the number of digits to the processor, it returns the number of digits to run the program, and returns 32 if the program is run in 32-bit mode on 64-bit Windows.

3. Use of PInvoke and GetSystemInfo

Note: In order to keep the article not too long. I did not include the PInvoke API statement (translator: C # Interoperability), but you might find it in the source code I provided.

ProcessorArchitecture pbits = Processorarchitecture.unknown;
Try
{
System_info l_system_info = new System_info ();
GetSystemInfo (ref l_system_info);
Switch (l_System_Info.uProcessorInfo.wProcessorArchitecture)
{
Case 9://PROCESSOR_ARCHITECTURE_AMD64
Pbits = Processorarchitecture.bit64;
Break
Case 6://Processor_architecture_ia64
Pbits = processorarchitecture.itanium64;
Break
Case 0://Processor_architecture_intel
Pbits = Processorarchitecture.bit32;
Break
Default://Processor_architecture_unknown
Pbits = Processorarchitecture.unknown;
Break
}
}
Catch
{
Ignore
}
return pbits;


Old question, it will return the number of digits of the running program, not the number of OS/processor digits.

4. Use of PInvoke and Getnativesysteminfo

I've seen people say it's not believable. You can use Getnativesysteminfo instead, the code is the same as above, just replace GetSystemInfo with Getnativesysteminfo.

The result is different. But... This API returns the number of digits in the processor itself, and I'm interested in the number of digits in the OS. After all, it's easy to run a 32-bit operating system on a 64-bit processor

5. Combination of Intptr.size and iswow64process

static public Softwarearchitecture Osbits
{
Get
{
Softwarearchitecture osbits = Softwarearchitecture.unknown;

Switch (intptr.size * 8)
{
Case 64:
Osbits = Softwarearchitecture.bit64;
Break

Case 32:
if (Is32bitprocesson64bitprocessor ())
Osbits = Softwarearchitecture.bit64;
Else
Osbits = Softwarearchitecture.bit32;
Break

Default
Osbits = Softwarearchitecture.unknown;
Break
}

return osbits;
}
}

private static Iswow64processdelegate Getiswow64processdelegate ()
{
INTPTR handle = LoadLibrary ("kernel32");

if (handle!= IntPtr.Zero)
{
IntPtr fnptr = GetProcAddress (handle, "iswow64process");

if (fnptr!= IntPtr.Zero)
{
Return (iswow64processdelegate) marshal.getdelegateforfunctionpointer (INTPTR) fnptr,
typeof (Iswow64processdelegate));
}
}

return null;
}

private static bool Is32bitprocesson64bitprocessor ()
{
Iswow64processdelegate fndelegate = Getiswow64processdelegate ();

if (fndelegate = null)
{
return false;
}

BOOL IsWow64;
BOOL RetVal = Fndelegate.invoke (Process.getcurrentprocess (). Handle, out isWow64);

if (RetVal = = False)
{
return false;
}

return isWow64;
}

If the IntPtr is 64, the operating system must also be 64 digits, because you cannot run 64-bit programs on 32-bit operating systems
If the program is running in 32-bit mode, the code detects whether it is a 64-bit processor, and the program runs in 32-bit mode to determine if it is 32-bit or 64-bit.
If 64 is returned, then the operating system is 64 bits, but the program runs in 32-bit mode, and if it is 32, then the operating system is 32.

Finally, I have added some methods to lib. To differentiate the number of programs/operating system/processor digits.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.