C# winform 文件管理
1.FolderBrowserDialog 打开文件夹中默认路径下的excl文件
private void button7_Click(object sender, EventArgs e) { FolderBrowserDialog fbd = new FolderBrowserDialog(); fbd.SelectedPath = "D:";//默认打开d盘 if (fbd.ShowDialog() == DialogResult.OK) { textBox1.Text = fbd.SelectedPath; DirectoryInfo dif = new DirectoryInfo(textBox1.Text); FileInfo[] fis = dif.GetFiles();//取所有文件信息 foreach (FileInfo fi in fis) { if (fi.Name.ToLower().Contains(".xls") || fi.Name.ToLower().Contains(".xlsx")) { listView1.Items.Add(fi.Name); } } } }
2.OpenFileDialog 打开excel 且可多选
OpenFileDialog fd = new OpenFileDialog(); fd.Multiselect = true; fd.Filter = "所有的excel文件|*.xls;*.xlsx"; if (fd.ShowDialog() == DialogResult.OK) { //只显示名称+扩展名 foreach (string s in fd.SafeFileNames) { listView1.Items.Add(s); } //显示全路径 foreach (string s in fd.FileNames) { listView1.Items.Add(s); } };