We help IT Professionals succeed at work.

open executable without using the drive letter


Does not work
Start("Program.exe")
This does not work either
System.Diagnostics.Process.Start("Program.exe")

Only this works...but I can't use the Drive path..cannot hardcode it.
   System.Diagnostics.Process.Start("C:\Program Files\X-Program\Program.exe")
So how do I start an outside Exe file from within vb net without hardcoding the path.
Comment
Watch Question

Adam MenkesC# ASP.NET Developer
Top Expert 2010

Commented:
1) You could use a folder picker.  or file picker...
2) You could store the path in a text file / config file / xml file in the same directory as your program, which would be hard-coded, and then open that file to get the path.

Without it, how would you know if you were supposed to execute
c:\temp\backups\Monday\Program.exe
c:\temp\backups\Tuesday\Program.exe
...
c:\temp\backups\20100228\Program.exe
etc?

Author

Commented:
No, that won't work. The external program is not mine and it will install to wherever the user installs it.
 System.Diagnostics.Process.Start("C:\Program Files\X-Program\Program.exe")
I have been able to do this in the past with VB6 so it has to be possible in vb net.
I have tried Shell, didn't work either.

Author

Commented:
how do you get the root path to an executable?
Adam MenkesC# ASP.NET Developer
Top Expert 2010

Commented:
The currently running executable?

Author

Commented:
 
I am able to open the calculator using the code below...so it has to be possible to shell it somehow, I am not sure of the root path to the"  external" executable without hardcoding.

---------------------------------------------------------------------------------------------

 Dim processID As Integer
        ' Start the Calculator application, and store the process id.
    processID = Shell("CALC.EXE", AppWinStyle.NormalFocus)
        '
        ' Activate the Calculator application.
   AppActivate(processID)

Author

Commented:
I am running a vb net app..my app. I want to open an external executable from within my program without hardcoding the path. I have tried quite a few things but so far nothings working.
Adam MenkesC# ASP.NET Developer
Top Expert 2010

Commented:
The problem is, that you will need the path to the EXE, or it will need to be in your PATH environmental variable.

The reason Calc.exe works, is that it is most likely in your path statement. Open a DOS prompt and type SET PATH and see if you have C:\Windows\System32 in there, and do the following:

Copy Calc.exe to somewhere else, like, c:\temp (as long as c:\temp is not in the path).
rename Calc.exe to Calc.exx in the windows\system32 directory

Try your shell command. It should NOT work.

You need a path to the EXE, either in your path settings on the computer, or in your program.
Senior Software Engineer
Commented:
When we uses Process.Start() it searches the file in BIN folder of project and System32 as well.So, if you know that you are searching .exe file into Program Folder you can do like this:

Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) & "\" & Filename)


SpecialFolder has some predefined locations.



Author

Commented:
That sounds good but I am not sure of how to declare it...and thats what it's now asking for.

Author

Commented:
Dim Program.exe as ????

Author

Commented:
Well, I found the answer and it works:
Try it yourselves. It does not need the C drive. It does not need to harcode the path to the drive.
If you know the basic path ( as Shahan said) you can find the exe without hardcoding the drive.

System.Diagnostics.Process.Start(String.Format("{0}\MyProgram\MyProgram.exe", Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)))

Author

Commented:
You were on the right path, just needed a tweak.