Results 1 to 4 of 4

Thread: Process.Start - Set Start In Directory

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2012
    Posts
    32

    Process.Start - Set Start In Directory

    Good evening. I am writing an application that in the end needs to execute another executable at some point, the problem is that the executable has dependencies in the directory it runs in that are required to operate. When I use the Process.Start to run the executable it errors out because it's looking for the dependencies in my applications directory.

    I tried the startInfo.WorkingDirectory option but that didn't seem to do it either (not sure if I did it right) - here is how I used that:

    Process.Start("my.exe").StartInfo.WorkingDirectory = "C:\Test"

    For the life of me I cannot find how to set the "Start In" directory. I looked everywhere, so unless it's under a different name, I'm at a loss.

    Any help is appreciated!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Process.Start - Set Start In Directory

    You can't set the start info after the process has started. Create a ProcessStartInfo object first, set the appropriate properties and then pass that object as an argument when you call Process.Start.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2012
    Posts
    32

    Re: Process.Start - Set Start In Directory

    Quote Originally Posted by jmcilhinney View Post
    You can't set the start info after the process has started. Create a ProcessStartInfo object first, set the appropriate properties and then pass that object as an argument when you call Process.Start.
    Oh geez - it was that easy! I knew I was wrong somewhere in the use of the WorkingDirectory setup. Thanks I really appreciate it. For those who might (I doubt) have the same problem, the code to fix it was:

    Dim ClientStartup As New ProcessStartInfo("yourexe.exe")
    ClientStartup.WorkingDirectory = "C:\YourDirectory"
    Process.Start(ClientStartup).WaitForExit()

    Thanks again.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Process.Start - Set Start In Directory

    If you're using VB 2008 or later then you can use initialiser syntax to condense that to:
    Code:
    Process.Start(New ProcessStartInfo("file path here") With {.WorkingDirectory = "folder path here"}).WaitForExit()
    Note also that, while it may not be, the working directory will usually be the folder containing the EXE, so you can do this:
    Code:
    Dim filePath As String 'Store file path here.
    
    Process.Start(New ProcessStartInfo(filePath) With {.WorkingDirectory = IO.Path.GetDirectoryName(filePath)}).WaitForExit()
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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