none
Running external .exe files RRS feed

  • Question

  • So I have code that runs exe files. However any time I have a space in the path of the file I get an error file not found.

    I tried the following 

     Dim procStartInfo As New ProcessStartInfo
            Dim procExecuting As New Process
    
            With procStartInfo
                .UseShellExecute = True
                .FileName = "C:\My Folder\File.exe"
                .WindowStyle = ProcessWindowStyle.Normal
                .Verb = "runas" 'add this to prompt for elevation
            End With
    
            procExecuting = Process.Start(procStartInfo)
            procExecuting.WaitForExit()
    
    'Also tried the following
    '.FileName = "C:\" & "'" & "My Folder\" & "'" & "File.exe"
    '.FileName = "C:\" & """"My Folder\"""" & "File.exe"
    '.FileName = "C:\" & ""My Folder\"" & "File.exe"
    
    

    The only work around I found so far is:

     Dim filebat As String = "C:" & vbCrLf &
                  "cd.." & vbCrLf &
                  "cd.." & vbCrLf &
                  "cd.." & vbCrLf &
                  "cd.." & vbCrLf &
                  "cd.." & vbCrLf &
                  "cd ""My Folder""" & vbCrLf &
                  "File.exe"
    
      My.Computer.FileSystem.WriteAllText("C:\file.bat", filebat, False, System.Text.Encoding.ASCII)
            Threading.Thread.Sleep(200) 'wait for file
    
    
     Dim procStartInfo As New ProcessStartInfo
            Dim procExecuting As New Process
    
            With procStartInfo
                .UseShellExecute = True
                .FileName = "C:\file.bat"
                .WindowStyle = ProcessWindowStyle.Normal
                .Verb = "runas" 'add this to prompt for elevation
            End With
    
            procExecuting = Process.Start(procStartInfo)
            procExecuting.WaitForExit()

    But I know I should have to do this. It is weird because I know I have executed files with spaces in folder name.

    I did upgrade to visual studio 2019 so wondering if this is a bug.

    Friday, April 19, 2019 5:45 PM

Answers

  • Hi

    Tried your code and both with and without the prompt for elevation, everything worked fine. I am using VB Community 2017.


    Regards Les, Livingston, Scotland

    • Marked as answer by Broggy69 Friday, April 26, 2019 5:14 PM
    Friday, April 19, 2019 6:15 PM

All replies

  • Hi

    Tried your code and both with and without the prompt for elevation, everything worked fine. I am using VB Community 2017.


    Regards Les, Livingston, Scotland

    • Marked as answer by Broggy69 Friday, April 26, 2019 5:14 PM
    Friday, April 19, 2019 6:15 PM
  • Hi

    Tried your code and both with and without the prompt for elevation, everything worked fine. I am using VB Community 2017.


    Regards Les, Livingston, Scotland

    Weird, because in 2019 it is not working, but then looking at my full code I am running other .exe with spaces in path and it works, but this one isn't. I know the path is correct as I copied it from windows explorer.

    I am loosing my mind. lol

    I will keep playing around, unless anyone else has an idea.

    Friday, April 19, 2019 6:34 PM
  • Hi

    Only thing would be to put a break point on Process.Start and check path/file name/extension in procStartInfo to make sure that they are correct. (I suspect you have tried this already, but only thing I can come up with)


    Regards Les, Livingston, Scotland


    • Edited by leshay Friday, April 19, 2019 7:11 PM
    Friday, April 19, 2019 7:09 PM
  • Hi

    Only thing would be to put a break point on Process.Start and check path/file name/extension in procStartInfo to make sure that they are correct. (I suspect you have tried this already, but only thing I can come up with)


    Regards Les, Livingston, Scotland


    Yep did that. When I get home I will try with 2017. Unless I need add a reference.
    Friday, April 19, 2019 7:48 PM
  • Hi

    Another thing occurred to me. Try putting the Break Point on the Process.Start line, copy the file name from procStartInfo  and paste into file explorer - execute it there and see if any issue finding the file that way.


    Regards Les, Livingston, Scotland

    Friday, April 19, 2019 8:10 PM
  • Hello,

    For a simple test try the following

    https://1drv.ms/u/s!AtGAgKKpqdWjjR6oPVCt0AYmienb

    By building this project alone will create a folder in the Bin\Debug folder (done in the post build event) then run the app to see if you get an error opening a file in this new folder. I stripped it down to creating a text file then opening it, nothing fancy, just want to see if this simple aspect works.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, April 19, 2019 9:34 PM
    Moderator
  • Hi

    Tried your code and both with and without the prompt for elevation, everything worked fine. I am using VB Community 2017.


    Regards Les, Livingston, Scotland

    Confirmed it works in 2017
    Friday, April 26, 2019 5:14 PM