1.创建选择文件夹对话框
1 List<FileInfo> ImageFileInfo = new List<FileInfo>(); //文件信息
2 FolderBrowserDialog fbd = new FolderBrowserDialog(); //选择文件夹
3 if (fbd.ShowDialog() == DialogResult.OK)
4 {
5 directory.Text = fbd.SelectedPath; //显示目录
6 CheckImageFile(fbd.SelectedPath,True) //根目录还是全部目录
7 }
8 void CheckImageFile(string TargetPath, bool TopOption)
9 {
10 if (TargetPath == "")
11 {
12 return;
13 }
14
15 var Option = TopOption == true ? SearchOption.TopDirectoryOnly : SearchOption.AllDirectories;
16
17 DirectoryInfo di = new DirectoryInfo(TargetPath);
18 InspectImageFileInfo.Clear();
19 InspectImageFileInfo.AddRange(di.GetFiles("*.jpg", Option));
20 InspectImageFileInfo.AddRange(di.GetFiles("*.jpeg", Option));
21 InspectImageFileInfo.AddRange(di.GetFiles("*.bmp", Option));
22 InspectImageFileInfo.AddRange(di.GetFiles("*.png", Option));
23
24 lbl_ImageCount.Text = "Image Count : " + InspectImageFileInfo.Count;
25 }
2.创建选择文件对话框
using System.Windows.Forms;
1 OpenFileDialog dlg = new OpenFileDialog();
2 dlg.Filter = "图片文件(*.png;*.jpg;*.bmp;*.jpeg)|*.png;*.jpg;*.bmp;*.jpeg"; //过滤文件后缀
3 dlg.Multiselect = true; //是否可以选择多张图片
4 if (dlg.ShowDialog() == DialogResult.OK)
5 {
6 this.PicPath = dlg.FileName; //获取单个文件名
7 }
1 List<string> FileNames = new List<string>(); OpenFileDialog dlg = new OpenFileDialog();
2 dlg.Filter = "图片文件(*.png;*.jpg;*.bmp;*.jpeg)|*.png;*.jpg;*.bmp;*.jpeg";
3 dlg.Multiselect = true;
4 if (dlg.ShowDialog() == DialogResult.OK)
5 {
6 this.PicPath = dlg.FileName; //获取首个文件名
7 }
8 for(int i=0;i<dlg.FileNames.Length;i++) //遍历所有文件名
9 {
10 FileNames.Add(dlg.FileNames.GetValue(i).ToString()); //加入List<string>
11 }
|
请发表评论