C# 폴더 및 하위 폴더 내 파일 반복 삭제

2611 단어 폴더
/// <summary>

        ///  

        /// </summary>

        /// <param name="str"></param>

        public void DeleteFiles(string str)

        {

             DirectoryInfo fatherFolder = new DirectoryInfo(str);

            // 

             FileInfo[] files = fatherFolder.GetFiles();

             foreach (FileInfo file in files)

             {

                 //string fileName = file.FullName.Substring((file.FullName.LastIndexOf("\\") + 1), file.FullName.Length - file.FullName.LastIndexOf("\\") - 1);

                 string fileName = file.Name;

                 try

                 {

                     if (!fileName.Equals("index.dat"))

                     {

                         File.Delete(file.FullName);

                     }                     

                 }

                 catch (Exception ex)

                 {

                 }

             }

            // 

             foreach (DirectoryInfo childFolder in fatherFolder.GetDirectories())

             {

                DeleteFiles(childFolder.FullName);

             }

            

 

        }

 

좋은 웹페이지 즐겨찾기