winform implements a simple example of the Select File and Select Folder dialog box
- 2021-12-04 10:56:27
- OfStack
Examples are as follows:
// Select a file, click Browse, and select a file
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog(); // Displays the Select File dialog box
openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
this.textBox1.Text = openFileDialog1.FileName; // Show file path
}
}
// Select Folder: Click "Browse" to select a folder
private void button4_Click(object sender, EventArgs e)
{
if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
if (this.folderBrowserDialog1.SelectedPath.Trim() != "")
this.textBox4.Text = this.folderBrowserDialog1.SelectedPath.Trim();
}
}
Select the folder dialog box, and if you want to default to 1 folder, add the following code starting with click Event 1:
folderBrowserDialog1.SelectedPath = " d:\ " ;
Hehe, isn't it very simple?