递归只删除文件夹内的所有文件

        private void DelFiles(string path)
        {
            try
            {               
                if (Directory.Exists(path))
                {
                    string[] filePath = Directory.GetFileSystemEntries(path);
                    if (filePath.Length > 0)
                    {
                        foreach (var fPath in filePath)
                        {
                            if (File.Exists(fPath))
                            {                               
                                try
                                {
                                    FileInfo dlefile = new FileInfo(fPath);
                                    dlefile.Delete();
                                }
                                catch (Exception ex)
                                {
                                    MessageBox.Show(ex.Message);
                                }
                            }
                            else
                            {                              
                                if (Directory.GetFileSystemEntries(fPath).Length>0)
                                {
                                    DelFiles(fPath);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

 

posted @ 2016-11-09 15:54  改变你我  阅读(320)  评论(0编辑  收藏  举报