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 }
