During winform development, the open file dialog box (openfiledialog) and folder browse dialog box (folderbrowserdialog) are often used ).
1. Folder browsing dialog box (folderbrowserdialog)
Step 1: Introduce a folderbrowserdialog component from the toolbox. When this component is added to the form, it will appear in a blank area below the form,
Step 2: You need a button to activate the folder. For this button btnselectpath, we use the following method:
Private void btnselectpath_click (Object sender, eventargs e) {folderbrowserdialog1.description = "select folder"; folderbrowserdialog1.shownewfolderbutton = true; folderbrowserdialog1.rootfolder = environment. specialfolder. desktop; dialogresult result = folderbrowserdialog1.showdialog (); If (result = dialogresult. OK) {txtmaindir. TEXT = folderbrowserdialog1.selectedpath ;}}
2. open the file dialog box (openfiledialog)
Step 1: Introduce the openfiledialog component, as shown in
Step 2: write code for the button btnselectfile:
private void btnSelectFile_Click(object sender, EventArgs e){ openFileDialog1.Filter = "Special Files(*.xls)|*.xls|All files (*.*)|*.*"; DialogResult result = openFileDialog1.ShowDialog(); if (result == DialogResult.OK) { txtFile.Text = openFileDialog1.FileName; }}
Over.