27. dubna 2012 19:03
Can anyone recommend a solution to my problem - I have a button on a form that is supposed to run an executable (compiled Intel Fortran v11) on the click event. Here is the code:
Catch ex As Exception
MessageBox.Show("Unable to locate desired file")
I'm certain the path is correct and I have verified that running the testCode.exe from the cmd window works as it should (testCode.out file is updated). When I try to run the process via the RunButton it appears to work...no errors are reported or caught. However, the expected output file is not updated indicating testCode.exe did not execute. All of the online support I have researched indicates this should work. Please help.
27. dubna 2012 19:23Moderátor
calling Process.Start() is more like double clicking the exe than running it from a command prompt. If you double click the fortran exe in windows explorer, does the testCode.out file get updated there too?
You may need to try launching the process using a ProcessStartInfo object, and messing with the properties of that, like setting UseShellExecute to False.
Matt Kleinwaks - MSMVP MSDN Forums Moderator - www.zerosandtheone.com
27. dubna 2012 23:08Yes, the testCode.out is generated if I simply double click the EXE. I will look into the second part of your reply over the weekend and I'll repost Monday. Thanks and have a nice weekend.
30. dubna 2012 12:55
It sounds like you are running a DOS app. See if the below link helps:
Paul ~~~~ Microsoft MVP (Visual Basic)
30. dubna 2012 17:44
It is a DOS app (compiled Intel Fortran code I normally run manually from the command prompt). I've tried creating the ProcessStartInfo object and setting the properties in different combinations...this hasn't worked. Several sites, including the vb-helper site, recommend this as the solution to running s DOS EXE from VB2010. So, here is where I am although it still isn't working:
Sub RunButton_Click() Handles RunButton.Click Dim start_info As New ProcessStartInfo("C:\testCode.exe") start_info.UseShellExecute = False start_info.CreateNoWindow = True start_info.RedirectStandardOutput = True start_info.RedirectStandardError = True ' Make the process and set its start information. Dim proc As New Process() proc.StartInfo = start_info ' Start the process. proc.Start() End SubSince there are no input arguments or screen output I didn't use the StreamReader. Any suggestions?
30. dubna 2012 18:16
I suspect that the working directory was the problem.
Dim exePath As String = "C:\testDir\testCode.exe"Note that, if you're running Win7, because of UAC you most likely won't be able to access the root drive directly (i.e "C:\testCode.exe"). In this case, you need to put the exe in a directory, i.e C:\testDir\testCode.exe as shown in the code.
Dim start_info As New ProcessStartInfo()
.FileName = exePath
.WorkingDirectory = IO.Path.GetDirectoryName(exePath)
30. dubna 2012 18:36
Looked like that took care of it.
Just so I feel like I learned something here, can you tell me WHY we needed to specify the WorkingDirectory property when we already specified the full path via the FileName property?
Thank you, Stanav (and others).
30. dubna 2012 19:26
The is what the doc says:
The WorkingDirectory property behaves differently when UseShellExecute is true than when UseShellExecute is false. When UseShellExecute is true, the WorkingDirectory property specifies the location of the executable. If WorkingDirectory is an empty string, the current directory is understood to contain the executable.
When UseShellExecute is false, the WorkingDirectory property is not used to find the executable. Instead, it is used by the process that is started and only has meaning within the context of the new process.
If you are using Stanav's code then UseShellExecute defaults to True and the executable can be properly located.
I'm not sure why the link to the code I posted wouldn't work. That is, unless the Fortran app was trying to write to a folder that was protected by UAC (not the app folder).
Paul ~~~~ Microsoft MVP (Visual Basic)
30. dubna 2012 19:46Thanks for the additional info re: the WorkingDirectory property. I again tried setting .UseShellExecute = False and verified .WorkingDirectory = "" and again it did not run. Several other sites suggest this should work so I am stumped. However, VB2010 is rather new to me so perhaps there is something else I haven't considered or am failing to do. In any event, it seems to be working with .UseShellExecute = True and specifying the .WorkingDirectory.