【问题标题】:Detect 32-bit or 64-bit检测 32 位或 64 位
【发布时间】:2011-12-15 19:43:04
【问题描述】:

我想从我的程序内部启动一个程序,现在我可以相对容易地做到这一点,它可以使用:

protected void butVNC_ItemClick(object sender, EventArgs e)
{
   string str = @"C:\Program Files\RealVNC\VNC4\vncviewer.exe";
   Process process = new Process();
   process.StartInfo.FileName = str;
   process.Start();
}

但我的问题是,如果我的程序安装在 64 位操作系统上,则该文件路径不正确,因为它是 Program Files(x86),所以有没有办法检测和运行不同的代码或任何东西。

【问题讨论】:

    标签: c# 64-bit 32-bit


    【解决方案1】:

    您可以使用 %ProgramFiles% 环境变量指向正确的 Program Files 目录。它应该正确地指向正确的路径。

    一个例子:C# - How to get Program Files (x86) on Windows 64 bit

    【讨论】:

    • 加载程序文件,但我如何让它打开“\RealVNC\VNC4\vncviewer.exe”
    • 如果您使用 Environmet.expandenvironmentvariables 扩展路径 "%ProgramFiles%\RealVNC\VNC4\vncviewer.exe" -> msdn.microsoft.com/en-us/library/… 您将获得每个系统的正确路径,即使是 32 位或 64 位(如果 RealVNC\VNC4\vncviewer.exe 在 64 位中没有变化)。
    • 我已经在顶部发布了我使用的代码,但是我感谢你提供的帮助,我只是无法使用它,因为我说我对这一切很抱歉:(
    【解决方案2】:

    我最终使用了这个,效果很好,而且非常简单:

            if (IntPtr.Size == 8)
            {
                string str = @"C:\Program Files(x86)\RealVNC\VNC4\vncviewer.exe";
                Process process = new Process();
                process.StartInfo.FileName = str;
                process.Start();
            }
    
            else if (IntPtr.Size == 4)
            {
                string str = @"C:\Program Files\RealVNC\VNC4\vncviewer.exe";
                Process process = new Process();
                process.StartInfo.FileName = str;
                process.Start();
            }
    

    感谢您的帮助:)

    【讨论】:

    • 这是不对的。如果在 64 位 Windows 上以 32 位运行,IntPtr.Size 将不会返回正确的值(它将返回 32 位)。因此,首先检查您是否在 64 位进程中运行,例如通过查看 IntPtr.Size。如果您在 32 位进程中运行,则应调用 Win API 函数 IsWow64Process。如果返回 true,则您正在 64 位 Windows 上的 32 位进程中运行。
    【解决方案3】:

    从 .NET 4.0 开始,您可以使用Environment.Is64BitProcess

    例子:

    if (Environment.Is64BitProcess)
    {
       // Do 64 bit thing
    }
    else
    {
       // Do 32 bit thing
    }
    

    【讨论】:

      猜你喜欢
      • 2011-10-24
      • 2019-03-28
      • 1970-01-01
      • 2017-08-04
      • 2012-10-26
      • 2010-12-17
      • 2012-10-25
      • 1970-01-01
      相关资源
      最近更新 更多