". Net" Select a local file in WinForm

Source: Internet
Author: User

Believe that many friends in the daily programming always encounter various problems, about choosing local files in the WinForm is a lot of friends are considered difficult to learn. NET difficult,

The FileUpload control is provided in WebForm to let us select the local file, so long as we drag the control onto the page, we already have the ability to select the local file. In WinForm, there is no control for us to integrate this functionality, but it provides us with the OpenFileDialog class, which we can use to open and select local files.

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

Property:
checkfileexists--The Settings dialog box displays a warning if the user-specified file name does not exist
multiselect--set whether users are allowed to select multiple files at the same time
readonlychecked--Gets or sets a value that indicates whether the read-only check box is selected
safefilename--gets the filename and extension of the file selected in the dialog box. File name does not contain a path
safefilenames--gets an array of file names and extension names for all selected files in the dialog box. File name does not contain a path
showreadonly--Gets or sets a value that indicates whether the dialog box contains a read-only check box

Method:
OpenFile ()--Returns the System.IO.Stream of the read-only file selected by the user
Reset ()--Reset all properties to their default values

Let's look at an example that provides a user selection file:
One, drag a button to the WinForm form
Second, double-clicking the Button,vs automatically adds a Click event to the button and jumps to the event, adding the following code to the event:

private void Button10_click (object sender, EventArgs e)
{
Initializes a OpenFileDialog class
OpenFileDialog FileDialog = new OpenFileDialog ();

Determine if the user has selected the file correctly
if (filedialog.showdialog () = = DialogResult.OK)
{
Gets the suffix name of the user-selected file
string extension = Path.getextension (filedialog.filename);
Declaring the allowed suffix names
string[] str = new string[] {". gif", ". Jpge", ". jpg"};
if (! ( (IList) str). Contains (extension))
{
MessageBox.Show ("can only upload images in gif,jpge,jpg format! ");
}
Else
{
Gets the file selected by the user and determines that the file size cannot exceed 20k,fileinfo.length in bytes.
FileInfo FileInfo = new FileInfo (filedialog.filename);
if (Fileinfo.length > 20480)
{
MessageBox.Show ("uploaded images cannot be larger than 20K");
}
Else
{
Here you can write the code after getting the correct file.
}
}
}
}
If we want to filter the file type in the pop-up selection box, you can set the Filter property of the OpenFileDialog. For example, we only allow users to select the. xls file, which can be set as follows:

Filedialog.filter = "(*.xls) |*.xls";

". Net" Select a local file in WinForm

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.