如何制作浏览文件夹的按钮?

时间:2015-09-08 06:39:45

标签: c# winforms visual-studio

我尝试搜索文件夹,当我发现在textBox1中复制地址时。我有下一个代码,但这不能正常工作,使用此代码我只找到文件。我的问题是:如何更改代码以创建用于查找文件夹的浏览按钮,以及何时找到复制textBox1中的地址?

private void browse_Click(object sender, EventArgs e) 
{
    OpenFileDialog fDialog = new OpenFileDialog();
    fDialog.Title = "Browse";
    fDialog.InitialDirectory = @"C:\LegacyApp\MATLAB\R2008a_64-bit";
    fDialog.Filter = "All files(*.*)|*.*|All files(*.*)|*.*";
    fDialog.FilterIndex = 2;
    fDialog.RestoreDirectory = true;
    if (fDialog.ShowDialog() == DialogResult.OK)
    {
        textBox1.Text = fDialog.FileName;               
    }
}

3 个答案:

答案 0 :(得分:3)

要浏览文件夹,您需要FolderBrowserDialog

private void browse_Click(object sender, EventArgs e) 
{
     if (folderBrowserDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
     {
         textBox1.Text = folderBrowserDialog1.SelectedPath;
     }
}

答案 1 :(得分:1)

尝试类似以下内容

textbox1.Text = fdialog.FileName.Substring(0, fdialog.FileName.lastIndexOf(@"\"));

答案 2 :(得分:0)

我尝试过如下,它为我工作。

private void browse_Click(object sender, EventArgs e) { var fDialog = new OpenFileDialog { Title = "Browse", InitialDirectory = @"C:\LegacyApp\MATLAB\R2008a_64-bit", Filter = "All files(*.*)|*.*|All files(*.*)|*.*", FilterIndex = 2, RestoreDirectory = true }; if (fDialog.ShowDialog() == DialogResult.OK) { textBox1.Text = fDialog.FileName; } }