none
How to delete a subdirectory and files only ? RRS feed

  • Question

  • I have a directory in this directory i have many files and also a sub directory. I want to delete only the files and the sub directory. But i want to keep the main directory without deleting it.

     

    private void getOutPutDirectoryFiles()
            {
                DirectoryInfo dir1;
                dir1 = new DirectoryInfo(_outputDir);
                FileInfo[] fileInfo = dir1.GetFiles();
                if (fileInfo.Length == 0)
                {
                }
                else
                {
                    for (int i = 0; i < fileInfo.Length; i++)
                    {
                        File.Delete(fileInfo[i].FullName);
                        Directory.Delete(_outputDir, true);
                    }
                }
                
            }
    


    I can delete all the files in the directory but how can i delete also the subdirectory ?

    So _outputDir will remain as it is and only the files inside and the subdirectories inside will be deleted.

    This Directory.Delete isnt a good option i guess.

    Thanks.


    danieli
    Sunday, November 13, 2011 6:15 PM

Answers

  • Reference Microsoft.VisualBasic and use Microsoft.VisualBasic.FileIO.FileSystem.DeleteDirectory Method (String, DeleteDirectoryOption)
    • Marked as answer by chocolade Sunday, November 13, 2011 10:35 PM
    Sunday, November 13, 2011 7:17 PM
  • Try this Code

            private void getOutPutDirectoryFiles()
            {
                DirectoryInfo dir1;
                dir1 = new DirectoryInfo(_outputDir);
                foreach (FileInfo fi in dir1.GetFiles())
                {
                    File.Delete(fi.FullName);
                }
                foreach (DirectoryInfo di in dir1.GetDirectories())
                {
                    Directory.Delete(di.FullName,true);
                }
            }



    Happy Coding, RDRaja
    • Edited by Dharmalinga Raja Sunday, November 13, 2011 7:41 PM
    • Marked as answer by chocolade Sunday, November 13, 2011 10:35 PM
    Sunday, November 13, 2011 7:40 PM

All replies

  • Reference Microsoft.VisualBasic and use Microsoft.VisualBasic.FileIO.FileSystem.DeleteDirectory Method (String, DeleteDirectoryOption)
    • Marked as answer by chocolade Sunday, November 13, 2011 10:35 PM
    Sunday, November 13, 2011 7:17 PM
  • Try this Code

            private void getOutPutDirectoryFiles()
            {
                DirectoryInfo dir1;
                dir1 = new DirectoryInfo(_outputDir);
                foreach (FileInfo fi in dir1.GetFiles())
                {
                    File.Delete(fi.FullName);
                }
                foreach (DirectoryInfo di in dir1.GetDirectories())
                {
                    Directory.Delete(di.FullName,true);
                }
            }



    Happy Coding, RDRaja
    • Edited by Dharmalinga Raja Sunday, November 13, 2011 7:41 PM
    • Marked as answer by chocolade Sunday, November 13, 2011 10:35 PM
    Sunday, November 13, 2011 7:40 PM