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

        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);
            }
        }
原文地址:https://www.cnblogs.com/gbnw/p/6047285.html