WinForm Control: Open File dialog box (OpenFileDialog)

Source: Internet
Author: User

The OpenFileDialog class provides the ability for a user to open a file, which has the following properties:

Property

InitialDirectory: Sets the initial directory of the dialog box.

Filter: 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, if the first item is selected, set to 1.

Restoredirectory: Controls whether the dialog box restores the current directory before closing.

FileName: The first file that is displayed in the dialog box or the last selected file.

Title: The name that will be displayed in the title bar of the dialog box.

MultiSelect: Set the Open dialog box to multiple selections.

1. Create a new WinForm form application named: Openfiledialogdemo

2. Add a button control on the interface (the window to open the file), add the text control for the log output (output the directory, file name, and file contents of the file that the end user opens).

3, the Background code implementation:

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.ComponentModel;4 usingSystem.Data;5 usingSystem.Drawing;6 usingSystem.IO;7 usingSystem.Linq;8 usingSystem.Text;9 usingSystem.Threading.Tasks;Ten usingSystem.Windows.Forms; One  A namespaceOpenfiledialogdemo - { -      Public Partial classForm1:form the     { -          PublicForm1 () -         { - InitializeComponent (); +         } -  +         /// <summary> A         ///Open File button click event at         /// </summary> -         /// <param name= "Sender" ></param> -         /// <param name= "E" ></param> -         Private voidBtn_openfile_click (Objectsender, EventArgs e) -         { -             //To define a file open control inOpenFileDialog OFD =NewOpenFileDialog (); -             //set the initial directory of the Open dialog box, the default directory is the path where EXE runs the file toOfd. InitialDirectory =Application.startuppath; +             //set the title of the Open dialog box -Ofd. Title ="Please select a file to open"; the             //Set Open dialog box to select multiple *Ofd. MultiSelect =true; $             //Settings dialog box open file typePanax NotoginsengOfd. Filter ="text files |*.txt| audio files |*.wav| picture files |*.jpg| All files |*.*"; -             //sets the index of the currently selected filter for the file dialog box theOfd. FilterIndex =2; +             //sets whether the dialog box remembers the previously opened directory AOfd. Restoredirectory =true; the             if(OFD. ShowDialog () = =DialogResult.OK) +             {  -                 //gets the full path of the file selected by the user $                 stringFilePath =OFD. FileName; $                 //gets the filename and extension of the file selected in the dialog box, and the filename does not include the path -                 stringFileName =OFD.             Safefilename;  -Outlog ("the file directory selected by the user is:"+FilePath); the                  -Outlog ("the file name selected by the user is:"+fileName);WuyiOutlog ("************** the contents of the selected file **************"); the                 using(FileStream fsread =NewFileStream (FilePath, FileMode.OpenOrCreate, FileAccess.Read)) -                 {  Wu                   //Defining binary Arrays -                     byte[] buffer =New byte[1024x768*1024x768*5]; About                     //Read bytes from stream $                     intR= fsread.read (Buffer,0, buffer. Length); -Outlog (Encoding.Default.GetString (buffer,0, R)); -                 } -             } A         } +  the         /// <summary> -         ///Output Log $         /// </summary> the         /// <param name= "Strlog" ></param> the         Private voidOutlog (stringStrlog) the         { the             //automatically empty if the log information is longer than 1000 lines -             if(Txt_fileinfo.getlinefromcharindex (Txt_FileInfo.Text.Length) > +) in             { the                 //Clear Text the txt_fileinfo.clear (); About             } theTxt_fileinfo.appendtext (DateTime.Now.ToString ("HH:mm:ss") +strlog+"\ r \ n"); the         } the     } +}

4. Generate EXE file test

5. Click the Open File button to open the dialog box. The default directory is the directory where the EXE files are located:

6, in the type of open file is we set the 4 and type, the default is selected is the 2nd audio file type:

7. After selecting the file to open, the program will run effect:

WinForm Control: Open File dialog box (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.