In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces Winform how to use OpenFileDialog pop-up file dialog box, the article is very detailed, has a certain reference value, interested friends must read it!
The OpenFileDialog class provides the ability for the user to open a file, which has the following properties:
Attribute
InitialDirectory: sets the initial directory of the dialog box.
Filter: the file filter to display in the dialog box, for example, "text files (* .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: controls whether the dialog box restores the current directory before closing.
FileName: the first file 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: the open dialog box can be selected in multiple settings.
1. Create a new Winform forms application named: OpenFileDialogDemo
2. Add a button control to the interface (the window for opening the file), and add the text control for log output (output the directory, file name and file content of the file opened by the end user).
3. 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 () Open file button Click event / private void btn_OpenFile_Click (object sender, EventArgs e) {/ / define a file opening 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 / / set the title of the open dialog box ofd.Title = "Please select the file to open"; / / set the open dialog box to select ofd.Multiselect = true; / / set the file type opened by the dialog box ofd.Filter = "text file | * .txt | audio file | * .wav | picture file | * .jpg | all files | *. *" / / set the index ofd.FilterIndex of the currently selected filter in the file dialog box = 2; / / set whether the dialog box remembers the previously opened directory ofd.RestoreDirectory = true; if (ofd.ShowDialog () = = DialogResult.OK) {/ / get the full path of the file selected by the user string filePath = ofd.FileName / / get the file name and extension of the selected file in the dialog box. The file name does not include the path string fileName = ofd.SafeFileName; OutLog ("the file directory selected by the user is:" + filePath); the file name selected by the user is: "+ fileName) OutLog ("* contents of selected file *"); using (FileStream fsRead = new FileStream (filePath, FileMode.OpenOrCreate, FileAccess.Read)) {/ / define binary array byte [] buffer = new byte [1024 * 1024 * 5] / / read bytes from the stream int r = fsRead.Read (buffer,0, buffer.Length); OutLog (Encoding.Default.GetString (buffer,0,r)) }} / output log / private void OutLog (string strLog) {/ / if the length of log information exceeds 1000 lines, automatically empty if (txt_FileInfo.GetLineFromCharIndex (txt_FileInfo.Text.Length) > 1000) {/ / clear text txt_FileInfo.Clear () } txt_FileInfo.AppendText (DateTime.Now.ToString ("HH:mm:ss") + strLog+ "\ r\ n");}
4. Generate exe file for testing
Click the Open File button to open the dialog box. The default directory is the directory where the exe file is located:
6. The type of open file is the 4 and type we set, and the second audio file type is selected by default:
7. After selecting the file to be opened, the running effect of the program:
The above is all the contents of the article "how to use OpenFileDialog pop-up file dialog box for Winform". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.