2016-03-08 15 views
-2

我試圖創建一個簡單的VB應用程序,讓我使用內置的Windows PowerShell掛載ISO文件。使用文本框的內容作爲Process.Start的參數

我的代碼是在這裏:

Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click 
    If OpenFileDialog1.ShowDialog = DialogResult.OK Then 

     TextBox1.Text = OpenFileDialog1.FileName 
     Me.ErrorProvider1.SetError(Me.TextBox1, "Looks Valid") 

    End If 
End Sub 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
    If ErrorProvider1.GetError(TextBox1) = "Looks Valid" Then 
     Process.Start("powershell", "Mount-DiskImage 'C:\ISO\MY Magic ISO.iso'") 

    Else 
     Me.ErrorProvider1.SetError(Me.TextBox1, "Please Select a Valid File") 

    End If 
End Sub 

我的程序很簡單,你選擇從對話框一個文件,如果文件是有效的,它安裝使用PowerShell。

我的問題是我將如何使用Textbox1的內容來動態更改「C:\ ISO \ MY Magic ISO.iso」文件路徑。這可能嗎?任何幫助將不勝感激,如果以前有人問我,我非常抱歉!

+1

有一些關於'如果ErrorProvider1.GetError(TextBox1)=「看起來有效」然後'看起來在概念上是錯誤的。 –

+0

這就是說,你有沒有嘗試過使用'TextBox1.Text'? –

+0

提示:[concatenation](https://msdn.microsoft.com/en-us/library/te2585xw.aspx)。 –

回答

0

分割字符串成片,並使用Concatenation把它們結合起來:

Process.Start("powershell", "Mount-DiskImage '" & TextBox1.Text & "'") 

樣品:

Module Module1 

    Sub Main() 

     Dim a As String = "C:\ISO\MY Magic ISO.iso" 
     Process.Start("powershell", "-noexit -Command ""Mount-DiskImage '" & a & "'""") 

    End Sub 

End Module 

證明的成功(或失敗):

Mount-DiskImage : The system cannot find the path specified. 
At line:1 char:1 
+ Mount-DiskImage 'C:\ISO\MY Magic ISO.iso' 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : ObjectNotFound: (MSFT_DiskImage:ROOT/Microsoft/.../MSFT_DiskImage) [Mount-DiskImage], Ci 
    mException 
    + FullyQualifiedErrorId : HRESULT 0x80070003,Mount-DiskImage 
+0

這工作100%,感謝你們提到的所有連隊,我一定會讀到這個! :) – Saaif

相關問題