Common methods for OpenFileDialog getting file names and file paths in C #

Source: Internet
Author: User

System.IO.Path.GetFullPath (Openfiledialog1.filename); Absolute path

System.IO.Path.GetExtension (Openfiledialog1.filename); File name extension

System.IO.Path.GetFileNameWithoutExtension (openfiledialog1.filename);//file name does not have an extension

System.IO.Path.GetFileName (Openfiledialog1.filename); Get the file

System.IO.Path.GetDirectoryName (Openfiledialog1.filename); Get path The return value of the function above is a string type.


The C # code for the Listener event for the button that selects the local file is attached below:


Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;

Using System.Windows.Forms; namespace Browsefile {public partial class Form1:form {public Form1 () {Initialize
        Component (); } private void Button1_Click (object sender, EventArgs e) {OpenFileDialog Fdlg = new OpenFile
            Dialog (); Fdlg.
            Title = "C # Corner Open File Dialog"; Fdlg.   InitialDirectory = @ "C:\"; @ is the meaning that cancels the escape character Fdlg. Filter = "All Files (*.*) |*.*|
            All Files (*.*) |*.* "; * * * FilterIndex property is used to select which file type, the default setting is 0, and the system takes the Filter property to set the first item *, which is equivalent to setting the FilterIndex property to 1. If you make up 3 file types, when Fil
             Terindex =2 refers to the 2nd one. * * Fdlg.
            FilterIndex = 2;  
             * * If the value is false, then the next selection of the file's initial directory is the last time you selected the directory, * is not fixed; If the value is true, the initial directory of each open dialog box does not change with your choice, is fixed * * FDLg.
            Restoredirectory = true; if (Fdlg. ShowDialog () = = DialogResult.OK) {TextBox1.Text = System.IO.Path.GetFileNameWithoutExtension ( Fdlg.
        
            FileName); 
 Refer http://blog.sina.com.cn/s/blog_7511914e0101cbjn.html to}}}}

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.