WinForm about how to use OpenFileDialog

Source: Internet
Author: User

The 1.OpenFileDialog control has the following basic properties
InitialDirectory Initial directory of the dialog box
Filter the file filter to display in the dialog box, for example, "text file (*.txt) |*.txt| All Files (*. *) | | *.*"
FilterIndex the index of the file filter selected in the dialog box, set to 1 if the first item is selected
Restoredirectory Control whether the dialog box restores the current directory before closing
FileName The first file to display in a dialog box or the last selected file
Title displays the characters in the title bar of the dialog box
AddExtension whether to add default extensions automatically
Checkpathexists Check if the specified path exists before the dialog box returns
DEFAULTEXT default name extension
Dereferencelinks whether to dereference a shortcut before returning from the dialog box
ShowHelp Enable the Help button
Validatenames the Control dialog box to check if the file name does not contain invalid characters or sequences

2.OpenFileDialog controls have the following common events
FileOk events to be handled when the user taps the "open" or "save" button
HelpRequest events to be handled when the user taps the Help button

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

Private voidOpenfiledialogbtn_click (Objectsender, System.EventArgs e) {OpenFileDialog OpenFileDialog=NewOpenFileDialog (); Openfiledialog.initialdirectory="c:\\";//Note that this path is written using c:\\ instead of C \Openfiledialog.filter="text Files |*.*| C # files |*.cs| all Files |*.*"; Openfiledialog.restoredirectory=true; Openfiledialog.filterindex=1; if(Openfiledialog.showdialog () = =DialogResult.OK) {fName=Openfiledialog.filename; File FileOpen=NewFile (fName); Isfilehavename=true; richTextBox1.Text=Fileopen.readfile (); Richtextbox1.appendtext (""); }}

The return of the path is a string type with filename
such as: Openfiledialog1.showdialog ();
_name1= Openfiledialog1.filename;
Image imge = Image.FromFile (_name1);
It uses the OpenFileDialog class, and here are some uses for this class!
The OpenFileDialog class is used to select the file location,
The FolderBrowserDialog class is used to select folder locations.
The specific code is as follows:
Program Source code:

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.IO;namespacecopyfile{ Public Partial classform1:form {String fileName;        String FolderName;        String Extendedname;               String fileName1;  PublicForm1 () {InitializeComponent (); }        Private voidBrowse_click (ObjectSender,eventargs e) {OpenFileDialog OFD=NewOpenFileDialog ();//New One methodOfd. InitialDirectory = environment. Getfolderpath_r (Environment.SpecialFolder.MyDocuments);//Define the default folder location to openOfd. ShowDialog ();//Show open File windowFileName = ofd. FileName;//get the selected file pathTextBox1.Text =FileName; Extendedname= Path. Getextension_r (FileName);//get file name extensionfileName1 = Path. Getfilename_r (FileName);//Get file name        }        Private voidFolder_click (Objectsender, EventArgs e) {FolderBrowserDialog FBD=NewFolderBrowserDialog (); Fbd.            ShowDialog (); FolderName= FBD. SelectedPath;//get the selected folder pathTextBox3.Text =FolderName; }        Private voidOK_Click (Objectsender, EventArgs e) {             if(TextBox1.Text.Trim (). Length = =0) {MessageBox.Show ("the file path cannot be empty!"); return; }            if(TextBox2.Text.Trim (). Length = =0) {MessageBox.Show ("the number of copies cannot be empty!"); return; }            if(TextBox3.Text.Trim (). Length = =0) {MessageBox.Show ("The destination folder path cannot be empty!"); return;                   } String NewFile; //define the location of the store, and the name of the store                        for(inti =1; I <= Convert.ToInt32 (TextBox2.Text); i++)//get the number of times to copy from TextBox2{newFile= FolderName +"\\"+ fileName1 +"_"+ i.tostring () +Extendedname; File.Copy (FileName, NewFile,true);//copy files using copy, copy (source file location, destination folder location, whether you can overwrite files with the same name)} MessageBox.Show ("copy done!"); }    }}

Add:

// Get file name Path. Getfilename_r (openfiledialog.filename)// Get the path to the file paths . Getdirectoryname_r (openfiledialog.filename)// Get file name extension Path. Getextension_r (Openfiledialog.filename)

WinForm How to use OpenFileDialog

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.