2013-12-20 164 views
-1

我想通過單擊按鈕打開文件對話框,但是我只希望它打開到我設置的文件路徑,並且用戶只能夠編輯該文件夾,這可能嗎?Visual Basic打開文件對話框

+0

到目前爲止,我已經能夠得到它打開到一個特定的路徑,但我也希望它被鎖定到該文件夾​​ – user3118124

+0

昏暗的fd作爲OpenFileDialog =新的OpenFileDialog fd.InitialDirectory =「文件路徑」 – user3118124

+0

嘗試按照@Steve答案使用'FileOk'事件讓我們知道 – equisde

回答

1

不,沒有辦法阻止您的用戶導航到其他目錄,如果他/她有權去那裏。 OpenFileDialog不提供任何基礎結構來阻止當前目錄。

有一個FolderBrowserDialog類,但這僅用於選擇文件夾而不選擇文件。

您可以打開對話框之前設置InitialDirectory財產和檢查,如果用戶選擇使用的FileOk事件的另一個目錄中的文件而無需關閉對話框

Dim openFileDialog1 As New OpenFileDialog() 
openFileDialog1.InitialDirectory = "c:\temp\testpath" 
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" 
openFileDialog1.FilterIndex = 2 
openFileDialog1.RestoreDirectory = True 
....... 


Private Sub openFileDialog1_FileOk(ByVal sender As Object, _ 
     ByVal e As System.ComponentModel.CancelEventArgs) _ 
     Handles OpenFileDialog1.FileOk 

    Dim ofd = CType(sender, OpenFileDialog) 
    if ofd.Filename <> string.Empty Then 
     if Path.GetDirectoryName(ofd.Filename).ToLower() <> "c:\temp\testpath" Then 
      MessageBox.Show("Please choose a file only from C:\TEMP\TESTPATH folder") 
      ' Just cancel the OK, not found any way to reposition the dialog on the correct folder' 
      e.Cancel = true 
     Endif 
    End If 
End Sub