文本框文本没有在VB.NET中用空格解析

时间:2013-05-22 17:44:54

标签: vb.net folderbrowserdialog system.management

我有一个文本框,将填充目录结构(例如,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."

1 个答案:

答案 0 :(得分: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