我正在尝试在VB中打开Wordpad和一个特定文件。
我无法弄清楚如何走上正确的道路。如果文件路径中没有空格(错误图像),则它可以工作。
正确的测试路径 C:\ Users \ James \ Documents \ Visual Studio 2015 \ Projects \ DSAinstaller \ DSAinstaller \ bin \ Debug \ Dragon
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If Me.ListBox1.SelectedIndex >= 0 Then
Dim curItem As String = ListBox1.SelectedItem.ToString()
Dim OpenFile As String = " " & Application.StartupPath & "\" & curItem & "\Inst.rtf"
'Shell("C:\Windows\write.exe" & " " & Application.StartupPath & "\" & curItem & "\Inst.rtf")
Shell("C:\Windows\write.exe" & OpenFile)
'MsgBox(OpenFile)
Else
MsgBox("Please select a program first")
End If
感谢
答案 0 :(得分:2)
如果路径包含空格,则需要在路径中添加引号:
Dim OpenFile As String = " """ & Application.StartupPath & "\" & curItem & "\Inst.rtf"""
答案 1 :(得分:0)
请考虑使用Path.Combine()和Process.Start():
Dim OpenFile As String = Chr(34) & Path.Combine(Application.StartupPath, curItem, "Inst.rtf") & Chr(34)
Process.Start("c:\windows\write.exe", OpenFile)
Shell()和MsgBox()是遗留函数,通常不应在VB.Net中使用。