How to detect Windows 64-bit platform with .NET?

前端 未结 29 3259
野性不改
野性不改 2020-11-22 04:53

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.ToS         


        
29条回答
  •  北荒
    北荒 (楼主)
    2020-11-22 05:28

    I'm using the followin code. Note: It's made for an AnyCPU project.

        public static bool Is32bitProcess(Process proc) {
            if (!IsThis64bitProcess()) return true; // We're in 32-bit mode, so all are 32-bit.
    
            foreach (ProcessModule module in proc.Modules) {
                try {
                    string fname = Path.GetFileName(module.FileName).ToLowerInvariant();
                    if (fname.Contains("wow64")) {
                        return true;
                    }
                } catch {
                    // What on earth is going on here?
                }
            }
            return false;
        }
    
        public static bool Is64bitProcess(Process proc) {
            return !Is32bitProcess(proc);
        }
    
        public static bool IsThis64bitProcess() {
            return (IntPtr.Size == 8);
        }
    

提交回复
热议问题