Winform code instance of the OpenFileDialog Popup file dialog box

Source: Internet
Author: User
This article mainly for you in detail the WinForm OpenFileDialog Open the File dialog box related information, with a certain reference value, interested in small partners can refer to

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:

Using system;using system.collections.generic;using system.componentmodel;using system.data;using System.Drawing; Using system.io;using system.linq;using system.text;using system.threading.tasks;using System.Windows.Forms;    namespace openfiledialogdemo{public partial class Form1:form {public Form1 () {InitializeComponent (); }///<summary>///Open File button click events///</summary>//<param name= "sender" ></param>// /<param name= "E" ></param> private void Btn_openfile_click (object sender, EventArgs e) {//define a file to open      Control OpenFileDialog OFD = new OpenFileDialog (); Set the initial directory of the Open dialog box, the default directory is the path where EXE runs the file OFD.      InitialDirectory = Application.startuppath; Sets the title OFD of the Open dialog box.      Title = "Please select a file to open"; The Set Open dialog box allows you to select multiple Ofd.      MultiSelect = true; The Settings dialog box opens the file type OFD.      Filter = "text file |*.txt| audio file |*.wav| picture file |*.jpg| all Files |*.*"; Sets the index OFD of the currently selected filter for the file dialog box.      FilterIndex = 2; Sets whether the dialog box remembers the previously opened directory OFD. RestoredIrectory = true; if (OFD. ShowDialog () = = DialogResult.OK) {//Gets the full path of the file selected by the user, string filePath = Ofd.        FileName; Gets the filename and extension of the file selected in the dialog box, and the file name does not include the path string filename = Ofd.               Safefilename;                Outlog ("User selected file directory is:" + FilePath);        Outlog ("User selected file name is:" +filename);        Outlog ("************** The contents of the selected file **************");          using (FileStream fsread = new FileStream (FilePath, FileMode.OpenOrCreate, FileAccess.Read)) {//define binary array          byte[] buffer = new BYTE[1024 * 1024 * 5]; Reads the byte int r= fsread.read (buffer, 0, buffer) from the stream.          Length);        Outlog (Encoding.Default.GetString (buffer,0,r));    }}}///<summary>//output Log///</summary>//<param name= "Strlog" ></param> private void Outlog (string strlog) {//If the log information is longer than 1000 lines, automatically empties if (Txt_fileinfo.getlinefromcharindex (txt_fil EInfo.Text.Length) > 1000) {//Empty text Txt_fileinfo.clear();    } txt_fileinfo.appendtext (DateTime.Now.ToString ("HH:mm:ss") +strlog+ "\ r \ n"); }  }}

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:

Related Article

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.