天天看點

【C#】擷取檔案、目錄下所有檔案、子目錄檔案

寫東西用到,記錄下。

内容是三個按鍵觸發,然後選擇目錄/檔案 擷取檔案資訊存放到listview中。

【C#】擷取檔案、目錄下所有檔案、子目錄檔案
//Click event of button bt_loadsub
        private void bt_loadsub_Click(object sender, EventArgs e)
        {
            openFileDialog1.Title = "Chose folder";
            if (openFileDialog1.ShowDialog() == DialogResult.OK || openFileDialog1.ShowDialog() == DialogResult.Yes)
            {            
                path = System.IO.Path.GetDirectoryName(openFileDialog1.FileName);                

                //擷取目錄下檔案名
                DirectoryInfo folder = new DirectoryInfo(path);
                foreach (FileInfo item in folder.GetFiles("*"+Path.GetExtension(openFileDialog1.FileName)))
                {
                    //MessageBox.Show("dictory" + item.Directory + "\n" + item.Name );
                    //re
                    //string pattern = @"(?<=HB(\d+).jpg-)[A-J]";//http://www.cnblogs.com/stg609/archive/2009/06/03/1492709.html
                    //MatchCollection mc = Regex.Matches(item.Name, pattern);
                    //MessageBox.Show(mc[].Value);
                    string[] arr = {
                        item.Directory.FullName,
                        item.Name,
                        item.FullName
                    };
                    listView1.Items.Add(new ListViewItem(arr));
                }
            }
        }

        //Click event of button bt_loadAll (sub folder
        private void bt_loadsubFolder_Click(object sender, EventArgs e)
        {

            FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();

            if(folderBrowserDialog1.ShowDialog() == DialogResult.OK || folderBrowserDialog1.ShowDialog() == DialogResult.Yes)
            {
                path = folderBrowserDialog1.SelectedPath;

                //擷取所有的子目錄
                DirectoryInfo di = new DirectoryInfo(path);//https://msdn.microsoft.com/en-us/library/s7xk2b58(v=vs.110).aspx
                DirectoryInfo[] diArr = di.GetDirectories("*.*", System.IO.SearchOption.AllDirectories);//https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/file-system/how-to-iterate-through-a-directory-tree

                foreach (DirectoryInfo dir in diArr)
                {
                    //MessageBox.Show("Test" + dir.Name + " "+ dir.Root + " " + dir.FullName);
                    //dir.FullName 就是目錄樹下的所有子目錄的檔案

                    foreach (FileInfo item in dir.GetFiles(this.tb_fileextension.Text) )
                    {
                        string[] arr = {
                            item.Directory.FullName,
                            item.Name,
                            item.FullName//mc[].Value
                        };
                        listView1.Items.Add(new ListViewItem(arr));
                    }//foreach
                }//foreach
            }//fi
        }//f

        //Click event of button bt_loadOne
        private void bt_loadOne_Click(object sender, EventArgs e)
        {
            openFileDialog1.Title = "Chose one";
            if(openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                FileInfo item = new FileInfo(openFileDialog1.FileName);

                string[] arr = {
                        item.Directory.FullName,
                        item.Name,
                        item.FullName
                    };
                listView1.Items.Add(new ListViewItem(arr));
            }
        }