2

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.

simonzack
  • 19,729
  • 13
  • 73
  • 118
Boyracer
  • 31
  • 4

3 Answers3

2

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

Example:

if (Environment.Is64BitProcess)
{
   // Do 64 bit thing
}
else
{
   // Do 32 bit thing
}
John Mills
  • 10,020
  • 12
  • 74
  • 121
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

Community
  • 1
  • 1
  • That loads the program files but then how do I get it to open "\RealVNC\VNC4\vncviewer.exe" – Boyracer Oct 26 '11 at 15:50
  • If you expand the path "%ProgramFiles%\RealVNC\VNC4\vncviewer.exe" using Environmet.expandenvironmentvariables -> http://msdn.microsoft.com/en-us/library/system.environment.expandenvironmentvariables.aspx you get the correct path for every system even is 32 or 64 bits (if the RealVNC\VNC4\vncviewer.exe does not changes in 64 bits). – Jon Ander Ortiz Durántez Oct 26 '11 at 16:17
  • I have posted the code i used at the top, however i appreciate the help you gave, i just couldnt get my head round using it, as i say im a noob to all this so sorry :( – Boyracer Oct 27 '11 at 09:53
1

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 :)

Boyracer
  • 31
  • 4
  • This isn't right. IntPtr.Size won't return the correct value if running in 32-bit on 64-bit Windows (it would return 32-bit). So first check whether you're running in a 64-bit process, for example by looking at IntPtr.Size. If you're running in a 32-bit process, you should then call the Win API function IsWow64Process. If this returns true, you're running in a 32-bit process on 64-bit Windows. – HTTP 410 Oct 27 '11 at 12:08