C # winform select a file, select a folder, open the file C # OpenFileDialog Open File dialog box (Detailed)

Friends of articles from the blog Park, just do my notes here.

Source: https://www.cnblogs.com/liuqifeng/p/9149125.html

First, select the file with OpenDialog

. 1 the OpenFileDialog Dialog = new new the OpenFileDialog ();
 2 dialog.Multiselect = to true ; // This value determines whether the plurality of files can be selected 
. 3 dialog.Title = " select the folder " ;
 . 4 dialog.Filter = " all files (*. *) | * *. " ;
 . 5  IF (dialog.ShowDialog () == System.Windows.Forms.DialogResult.OK)
 . 6  {
 . 7      String File = dialog.FileName;
 . 8 }
Filter property assigned to a string for the file type filter; 
string as follows: 
' | ' split two, is a comment, a Filter is true, the comment is displayed. If more than one type of file to display, separated by a semicolon. 
Such as: 
Open1.Filter = " image file (* .jpg, * GIF, BMP *..) | * .Jpg; * GIF; * BMP.. " ; 
The filter file type is " |" the right number of *. .. jpg; * gif; * bmp three types of files displayed in OpenDialog / SaveDialog in to see the user's file type string is: "|" number to the left of the image file (* .jpg, * GIF, *. . bmp). 
Another example: 
Open1.Filter = " image file (.... * .Jpg; * JPG; * jpeg; * GIF; PNG *) | * .jpg; * jpeg; * GIF; * PNG... " ;

 

Second, select the folder to use System.Windows.Forms.FolderBrowserDialog

. 1 System.Windows.Forms.FolderBrowserDialog Dialog = new new System.Windows.Forms.FolderBrowserDialog ();
 2 dialog.Description = " Choose where Txt file folder " ;
 . 3  IF (dialog.ShowDialog () == the System.Windows.Forms .DialogResult.OK)
 . 4  {
 . 5     IF ( String .IsNullOrEmpty (dialog.SelectedPath))
 . 6     {
 . 7         System.Windows.MessageBox.Show ( the this , " folder path can not be empty " , " prompt " );
 . 8         return ;
 . 9    }
10    this.LoadingText = "处理中...";
11    this.LoadingDisplay = true;
12    Action<string> a = DaoRuData;
13    a.BeginInvoke(dialog.SelectedPath,asyncCallback, a);
14 }

 

Third, directly open the file or folder at a certain path

 System.Diagnostics.Process.Start("ExpLore", "C:\\window");

 

Also you can refer to:

C # OpenFileDialog Open File dialog box (Detailed)

First, the Open File dialog box (OpenFileDialog)

1, the basic properties of the OpenFileDialog control

  • InitialDirectory: Dialog initial directory 
  • Filter:  Gets or sets the current file name filter string, for example, "Text Files (* .txt) | * .txt | All files (* *.) || * *  ."
  • FilterIndex selected in the dialog file filter the index, if you select the first set to 1 
  • RestoreDirectory control whether the dialog box restores the current directory before closing 
  • FileName: The first file is displayed in the dialog box or the last selected file 
  • Title character will appear in the dialog title bar 
  • Whether AddExtension automatically add the default extension 
  • CheckPathExists return before the dialog box, check the specified path exists 
  • DefaultExt default extension 
  • DereferenceLinks before returning from the dialog whether to dereference shortcuts 
  • ShowHelp enable the "Help" button 
  • ValiDateNames control character or whether the sequence check box does not contain invalid filename

2, OpenFileDialog control has the following common events

FileOk  When the user clicks to deal with "Open" or "Save" button event 
HelpRequest  When the user clicks the "Help" button to process the event

You can use the following code to implement the above dialog box:

Private  void openFileDialogBTN_Click ( Object SENDER, System.EventArgs E) 
{ 
    the OpenFileDialog OpenFileDialog = new new the OpenFileDialog (); 
    openFileDialog.InitialDirectory = " c: \\ " ; // Note that when writing to use path c: \\ instead of c: \ 
    = openFileDialog.Filter " . text file | * * | C # file | * .cs | All files | * *. " ; 
    openFileDialog.RestoreDirectory = to true ; 
    openFileDialog.FilterIndex = 1 ;
     IF (openFileDialog.ShowDialog () == DialogResult. the OK) 
    { 
        fName=openFileDialog.FileName;
        File fileOpen=new File(fName);
        isFileHaveName=true;
        richTextBox1.Text=fileOpen.ReadFile();
        richTextBox1.AppendText("");
    }
}

3, access to the file name of the dialog

openfiledialog.FileName // Gets or sets a selected file in the file dialog string containing the name

openfiledialog.SafeFileName // get the file name and extension selection dialog

Second, open the folder dialog (FolderBrowserDialog)

FolderBrowserDialog dialog = new FolderBrowserDialog();
dialog.Description = "请选择文件路径";

if (dialog.ShowDialog() == DialogResult.OK)
{
    savePath = dialog.SelectedPath;
    textBox2.Text = savePath;
}

 

Guess you like

Origin www.cnblogs.com/huashanqingzhu/p/11314533.html