J. Scott Elblein J. Scott Elblein - 6 months ago 78
Vb.net Question

Run a Relative Path in Windows?

I'm attempting to run a destination relative path from within a VB.NET app. I've made sure to use backslashes (rather than forward slashes), and also running the Process with the working directory set to the correct source path; still getting a

The system cannot find the file specified
error when trying to run it.

For example, I have (pseudo-code):

txtSource.text path = "C:\Windows\System32"

txtResult.text path = "..\notepad.exe"

Here's the Sub so far:

Private Sub btnTest_Click(sender As Object, e As EventArgs) Handles btnTest.Click


' Create the process object
Dim pRun As New Process()

' Set it to run from the Source folder (Working Directory)
With pRun.StartInfo
.UseShellExecute = False
.WorkingDirectory = IO.Path.GetDirectoryName(txtSource.Text.Trim)
.FileName = txtResult.Text.Trim
End With


' Wait for it to finish

Catch ex As Exception


End Try

End Sub


IO.Path.GetDirectoryName("C:\Windows\System32") returns "C:\Windows"; the directory that contains "C:\Windows\System32".

StartInfo.Filename = "..\notepad.exe" tells the process to look for notepad.exe in "C:\".

Also, for this to work, you need to set StartInfo.UseShellExecute = True; see: ProcessStartInfo Class for the description.

With pRun.StartInfo
    .UseShellExecute = True
    .WorkingDirectory = txtSource.Text.Trim
    .FileName = txtResult.Text.Trim
End With