Boyracer
Boyracer

Reputation: 31

Detect 32-bit or 64-bit

I want to launch a program from inside my program, now I can do this relatively easy, and it works using:

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();
}

But my problem is, if my program is installed on a 64-bit operating system, that file path is not right, as it is Program Files(x86) so is there a way to detect and run different code or anything.

Upvotes: 2

Views: 1330

Answers (3)

John Mills
John Mills

Reputation: 10263

From .NET 4.0, you can use Environment.Is64BitProcess.

Example:

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

Upvotes: 2

Boyracer
Boyracer

Reputation: 31

I ended up using this, and works well, and is really simple:

        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();
        }

Thank you for your help though :)

Upvotes: 1

You can use the %ProgramFiles% enviroment variable to point to the correct Program Files directory. It should point properly to the correct path.

An example : C# - How to get Program Files (x86) on Windows 64 bit

Upvotes: 1

Related Questions