When I was doing a C # project, I encountered the problem of getting the number of operating system bits, and had not found a more complete solution on the internet for several hours. Not much to say, directly on the code that can be run (simple, rude)
usingSystem.Runtime.ConstrainedExecution;usingSystem.Runtime.InteropServices;Internal Static classwin32native {[ReliabilityContract (Consistency.willnotcorruptstate, Cer.mayfail)] [DllImport ("Kernel32.dll", bestfitmapping =false, CharSet = CharSet.Auto, SetLastError =true)] Private Static externIntPtr GetModuleHandle (stringmodulename); [DllImport ("Kernel32.dll", bestfitmapping =false, CharSet = charset.ansi, exactspelling =true, SetLastError =true)] Private Static externIntPtr GetProcAddress (IntPtr hmodule,stringmethodName); [DllImport ("Kernel32.dll", SetLastError =true)] [return: MarshalAs (unmanagedtype.bool)]Internal Static extern BOOLIswow64process ([in] IntPtr hsourceprocesshandle, [MarshalAs (Unmanagedtype.bool)] out BOOLisWow64); [DllImport ("Kernel32.dll", CharSet = CharSet.Auto, SetLastError =true)] Internal Static externIntPtr getcurrentprocess (); Internal Static BOOLDoeswin32methodexist (stringModuleName,stringmethodName) {INTPTR Modulehandle=Win32native.getmodulehandle (modulename); if(Modulehandle = =IntPtr.Zero) {return false; } IntPtr procaddress=win32native.getprocaddress (Modulehandle, methodName); returnProcaddress! =IntPtr.Zero; } Public Static BOOLIs64bitoperatingsystem {Get { BOOLflag=default(BOOL); return(Win32native.doeswin32methodexist ("Kernel32.dll","iswow64process") && win32native.iswow64process (win32native.getcurrentprocess (), outFlag)) &Flag; } } }
C # Determines whether the operating system is 32-bit or 64-bit