[英]How I can make a button for browse a folder?
I try to search a folder and when I find to copy the address in a textBox1
.I have the next code, but this doesn't work properly, with this code I just find the files. 我尝试搜索文件夹,当我发现在
textBox1
复制地址。我有下一个代码,但这不能正常工作,使用此代码我只找到文件。 My question is : How can I change the code to make a browse button for find a folder and when I find to copy the address in 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;
}
}
To browse folder, you need FolderBrowserDialog 要浏览文件夹,您需要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; } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.