2014-08-27 82 views
0

我有一个C#库,其中包含我在其中使用文件夹浏览器对话框组件获取文件夹路径的窗体。 表单在使用自定义安装程序安装我的应用程序期间显示。 当点击浏览按钮显示文件夹浏览器对话框。对话框已打开,但没有文件夹列表,空白对话框显示为“确定”和“取消”按钮。我正在使用以下代码:文件夹浏览器对话框组件没有显示窗体中的文件夹列表

FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog(); 
folderBrowserDialog.RootFolder = Environment.SpecialFolder.MyComputer; 
if (folderBrowserDialog.ShowDialog() == DialogResult.OK) 
{ 
    txtDBPath.Text = folderBrowserDialog.SelectedPath; 
    btnSelectFile.Enabled = true; 
} 

我该如何解决此问题。谢谢

+1

“自定义安装程序”肯定是麻烦的。添加一个诊断:if(System.Threading.Thread.CurrentThread.GetApartmentState()!= System.Threading.ApartmentState.STA)MessageBox.Show(“它将是空的”); – 2014-08-27 09:41:44

回答

0

我解决了这个问题。

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click 
    Try 
     Dim MyThread As New Threading.Thread(AddressOf ShowMyFolderBrowserDialog) 
     MyThread.SetApartmentState(Threading.ApartmentState.STA) 
     MyThread.Start() 
    Catch ex As Exception 
     MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Setup") 
    End Try 
End Sub 

Private Sub ShowMyFolderBrowserDialog() 
    Try 
     Me.FolderBrowserDialog1.RootFolder = Environment.SpecialFolder.MyComputer 
     Me.FolderBrowserDialog1.Description = "Select folder" 
     If System.IO.Directory.Exists(Me.TextBox1.Text) Then 
      Me.FolderBrowserDialog1.SelectedPath = Me.TextBox1.Text 
     End If 
     If Me.FolderBrowserDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then 
      Me.TextBox1.Text = Me.FolderBrowserDialog1.SelectedPath 
     End If 
    Catch ex As Exception 
     MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Setup") 
    End Try 
End Sub