0

我有一个将使用目录结构填充的文本框(例如,C:\Program Files\Visual Basic)。我正在尝试textbox.text在另一个变量中使用该对象,但是当路径包含空格时,信息将被切断。

这是使用路径填充文本框的违规代码:

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles b_Script.Click
        Dim BrowseFolder As New FolderBrowserDialog
        BrowseFolder.ShowDialog()
        tbScript.Text = BrowseFolder.SelectedPath
    End Sub

请注意,它用于FolderBrowserDialog填充文本框,这部分工作正常。我有另一个按钮单击,然后textbox.text在我在其他地方定义的函数中使用特定文件名:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim Path As String
        Path = tbScript.Text & "\Client_Perfmon.ps1"
        RunScript(Path)
    End Sub

它永远无法解析文件名,例外是:

    Exception:Caught: "The term 'C:\Users\seanlon\Desktop\Performance\Powershell' is not
 recognized as the name of a cmdlet, function, script file, or operable program. Check the
 spelling of the name, or if a path was included, verify that the path is correct and try
 again." (System.Management.Automation.CommandNotFoundException)
    A System.Management.Automation.CommandNotFoundException was caught: "The term
 'C:\Users\seanlon\Desktop\Performance\Powershell' is not recognized as the name of a
 cmdlet, function, script file, or operable program. Check the spelling of the name, or if
 a path was included, verify that the path is correct and try again."
4

1 回答 1

1

您的第一个按钮处理程序应该检查 ShowDialog() 的结果:

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles b_Script.Click
    Dim BrowseFolder As New FolderBrowserDialog
    If BrowseFolder.ShowDialog() = Windows.Forms.DialogResult.OK Then
        tbScript.Text = BrowseFolder.SelectedPath
    End If
End Sub

您的第二个处理程序应该使用 Path.Combine():

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim Path As String = System.IO.Path.Combine(tbScript.Text, "Client_Perfmon.ps1")
    RunScript(Path)
End Sub

*它可能应该检查以确保路径也确实存在!

RunScript() 有什么作用?

有时应用程序要求带空格的路径用引号引起来:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim Path As String = Chr(34) & System.IO.Path.Combine(tbScript.Text, "Client_Perfmon.ps1") & Chr(34)
    RunScript(Path)
End Sub
于 2013-05-22T18:01:50.277 回答