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

本文关键字:按钮 文件夹 浏览 何制作 | 更新日期: 2023-09-27 18:11:35

我尝试搜索文件夹,当我发现在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;               
    }
}

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

要浏览文件夹,需要使用FolderBrowserDialog

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

尝试如下内容

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

我尝试过如下方法,它对我有效。

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; } }