C# 删除文件夹下的所有文件

public bool DeleteFiles(string path)
        {
            if (Directory.Exists(path)==false)
            {
                MessageBox.Show("Path is not Existed!");
                return false;
            }
            DirectoryInfo dir = new DirectoryInfo(path);
            FileInfo[] files = dir.GetFiles();
            try
            {
                foreach (var item in files)
                {
                    File.Delete(item.FullName);
                }
                if (dir.GetDirectories().Length != 0)
                {
                    foreach (var item in dir.GetDirectories())
                    {
                        if (!item.ToString().Contains("$") && (!item.ToString().Contains("Boot")))
                        {
                            // Console.WriteLine(item);

                            DeleteFiles(dir.ToString() + "\\" + item.ToString());
                        }
                    }
                }
                Directory.Delete(path);
                
                return true;
            }
            catch (Exception)
            {
                MessageBox.Show("Delete Failed!");
                return false;

            }



        }

 

posted @ 2013-01-21 22:44  louiskoo  阅读(16649)  评论(0编辑  收藏  举报