分离文件路径、名称和扩展名源代码:

所有文件源代码 名称和扩展名源代码(1)

程序界面

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //"E:\study\mydatabase\学生个人数据库.accdb" if (openFileDialog1.ShowDialog()==DialogResult.OK) { string f_all = openFileDialog1.FileName; string f_path = f_all.Substring(0, f_all.LastIndexOf("\\") 1); string f_name = f_all.Substring(f_all.LastIndexOf("\\") 1, f_all.LastIndexOf(".") - (f_all.LastIndexOf("\\") 1)); string f_ex = f_all.Substring(f_all.LastIndexOf(".") 1, f_all.Length - (f_all.LastIndexOf(".") 1)); label1.Text = "文件路径:" f_path; label2.Text = "文件名:" f_name; label3.Text = "扩展名:" f_ex; } } } }

结语:学会使用打开对话框控件openFileDialog和截取字符串方法Substring和获取特定字符索引号IndexOf(正序)和LastIndexOf(倒序),喜欢的请关注收藏!,