Results 1 to 5 of 5

Thread: Starting an EXE from within a VB.NET program

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2010
    Location
    Huntsville, AL
    Posts
    62

    Starting an EXE from within a VB.NET program

    Hello,

    I have an executable (EXE) that I would like to run from within my VB.NET program. I've looked into Process.Start("[PATH]\[EXE]"), where PATH is the path to my executable, and EXE is the name of the executable. However this throws an error, saying "The parameter is incorrect". I've managed to avoid this error with the following code:

    Code:
            Dim proc As New Process
            With proc.StartInfo
                .WorkingDirectory = _STFileInfo.DirectoryName
                .FileName = _STFileInfo.Name
            End With
    
            proc.Start()
    I should note that _STFileInfo is a variable of FileInfo type that holds the path of my executable. However, using the above code, all that happens is the command shell is opened then immediately closed. My executable doesn't appear to have run at all. I've also tried adding "proc.WaitForExit()" after "proc.Start()", but that doesn't seem to do anything.

    Any ideas?

  2. #2
    Addicted Member Programmation's Avatar
    Join Date
    Nov 2009
    Posts
    161

    Re: Starting an EXE from within a VB.NET program

    Your code:

    Code:
     Dim myFileInfo As New FileInfo(Application.StartupPath & "\EXE.exe")
            Dim myProcess As New Process
            myProcess.StartInfo.WorkingDirectory = myFileInfo.DirectoryName
            myProcess.StartInfo.FileName = myFileInfo.Name
            myProcess.Start()
    Or:

    Code:
    Process.Start(Application.StartupPath & "\EXE.exe")
    Good luck
    Just Do It!

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2010
    Location
    Huntsville, AL
    Posts
    62

    Re: Starting an EXE from within a VB.NET program

    Is it required that the executable reside within the application's startup directory? Otherwise I don't see any difference between your code and mine.

  4. #4
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: Starting an EXE from within a VB.NET program

    This example starts Wordpad, opens a file in the app folder (although it could be anyplace), sets the working folder to My Documents (but for Word pad it really does not matter).

    We only use the app folder since we know this folder exists and should be rights have permissions to this folder.

    Code:
    Private Sub Demo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDemo.Click
       Dim FileToRead As String = IO.Path.Combine(Application.StartupPath, "Document.txt")
       If Not IO.File.Exists(FileToRead) Then
          IO.File.WriteAllText(FileToRead, "Just created!!!")
       End If
       Dim regVersion As RegistryKey
       regVersion = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\wordpad.exe", True)
       If regVersion IsNot Nothing Then
          Dim proc As New Process
          With proc.StartInfo
             .FileName = regVersion.GetValue("").ToString
             .Arguments = IO.Path.Combine(Application.StartupPath, "Document.txt")
             .WorkingDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
          End With
          proc.Start()
       End If
    End Sub
    Now suppose WordPad did care about the working directory to find files if the working folder was incorrect it may not start and give no error message.

    You best bet to start with is to set a break point on your code to see what the values are for WorkingDirectory and FileName to ensure they are indeed valid.

  5. #5
    Lively Member agent_007's Avatar
    Join Date
    Jan 2010
    Posts
    93

    Re: Starting an EXE from within a VB.NET program

    try changing _STFileInfo.Name to _STFileInfo.FullName

    Im Pro in C#,VB.NET,Batch,Socket Programming, SPY Software Programming, VB6, Win32Api, VBscript, Windows Registry, ASP.NET, PHP, Jquery, AJAX.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width