递归得到指定文件夹下所有.mdb文件

时间:2022-05-15 09:57:05

 

        private void button3_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.ShowDialog();
            string path = folderBrowserDialog1.SelectedPath;
            if (!string.IsNullOrEmpty(path.Trim()))
            {
                textBox1.Text = path.Trim();
                getFilePath(path);
                label2.Text = "共有文件"+files.Count+"个";
            }
            else
            {
                label2.Text = "请选择正确的文件路径";
            }
        }

        /// <summary>
        /// 递归得到指定文件夹下所有.mdb文件
        /// </summary>
        /// <param name="path">指定文件夹</param>

        private void getFilePath(string path)
        {
            foreach (string file in Directory.GetFiles(path, "*.mdb"))//得到当前文件夹下所有文件路径
            {
                files.Add(file);
            }
            string[] dir = Directory.GetDirectories(path);//得到当前文件夹下所有子文件夹路径
            foreach (string direc in dir)
            {
                getFilePath(direc);
            }
        }