运行 Windows 中的相对路径?

Run a Relative Path in Windows?

我正在尝试从 VB.NET 应用程序中 运行 目标相对路径。我确保使用反斜杠(而不是正斜杠),并且 运行 将工作目录设置为正确的源路径;尝试 运行 时仍然出现 The system cannot find the file specified 错误。

比如我有(伪代码):

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

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

这是目前的子:

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

    Try

        ' 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

        pRun.Start()

        ' Wait for it to finish
        pRun.WaitForExit()

    Catch ex As Exception

        Debug.Print(ex.Message)

    End Try

End Sub

IO.Path.GetDirectoryName("C:\Windows\System32")returns"C:\Windows";包含 "C:\Windows\System32".

的目录

StartInfo.Filename = "..\notepad.exe" 告诉进程在 "C:\".

中查找 notepad.exe

此外,要使其正常工作,您需要设置 StartInfo.UseShellExecute = True;参见:ProcessStartInfo Class 的描述。

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