Reputation: 347
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. 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
?
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;
}
}
Upvotes: 3
Views: 3311
Reputation: 162
I have tried like follows, Its working for me.
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;
}
}
Upvotes: 0
Reputation: 5990
To browse folder, you need FolderBrowserDialog
private void browse_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
textBox1.Text = folderBrowserDialog1.SelectedPath;
}
}
Upvotes: 3
Reputation: 515
Try something like the following
textbox1.Text = fdialog.FileName.Substring(0, fdialog.FileName.lastIndexOf(@"\"));
Upvotes: 1