1

Possible Duplicate:
DirectoryInfo.Delete vs Directory.Delete

I made this code to empty some files that I regularly delete, such as temp and MSN cahes files in Windows.

code1

I can add new paths to apply DeleteContains method in easy way I just have to add the path to the list

code2

will not allow me to add paths as much I want and I have to create new array of string for each path and a new loop too

anyway to use code1 to delete contains of the folder NOT the folder and its contains??

any work around in the foreach in code1 to work as code2 ??

Because some Folder can be delete or deleting them will cause problem for some apps and the apps wont work again it like

"C:\Users\user\AppData\Local\Temp"

while other folder can be delete it with no problem like MSN cashes

"C:\Users\user\AppData\Local\Microsoft\Windows Live\Contacts"

code1

        private void Clear_Click(object sender, EventArgs e)
    {
        List<DirectoryInfo> FolderToClear = new List<DirectoryInfo>();

        // here is a list of files  I want to delete  

        FolderToClear.Add(new DirectoryInfo("path1"));
        FolderToClear.Add(new DirectoryInfo("path2"));
        FolderToClear.Add(new DirectoryInfo("path3"));
        FolderToClear.Add(new DirectoryInfo("path4"));

        foreach (DirectoryInfo directory in FolderToClear)
        {
            directory.Delete(true);
        }
    }

code2

        private void DeleteContents(string Path)
    {
        string[] DirectoryList = Directory.GetDirectories(Path1);
        string[] FileList = Directory.GetFiles(Path1);

        foreach (string xin FileList)
        {
            File.Delete(x);
        }
        foreach ( string x in DirectoryList)
        {
            Directory.Delete(x, true);
        }
    }
Community
  • 1
  • 1
xXghostXx
  • 67
  • 2
  • 6

1 Answers1

4

Try:

DirectoryInfo directory = new DirectoryInfo("Path");
foreach (FileInfo fi in directory.GetFiles())
{
    fi.Delete();
}

Better yet, here is a recursive that will delete all the files and sub directories under the DirectoryInfo you provide.

Note: There is nothing in here to handle file locks so it will bomb if you have th file open. This should get you started.

    public void KeepTheseFolders(DirectoryInfo dirInfo)
    {
        DeleteFolder(new DirectoryInfo("Path1"), false);
        DeleteFolder(new DirectoryInfo("Path2"), false);
        DeleteFolder(new DirectoryInfo("Path3"), false);
        DeleteFolder(new DirectoryInfo("Path4"), false);
    }

    public void DeleteFolder(DirectoryInfo dirInfo, bool deleteDirectory)
    {
        //Check for sub Directories
        foreach (DirectoryInfo di in dirInfo.GetDirectories())
        {
            //Call itself to delete the sub directory
            DeleteFolder(di, true);
        }

        //Delete Files in Directory
        foreach (FileInfo fi in dirInfo.GetFiles())
        {
            fi.Delete();
        }

        //Finally Delete Empty Directory if optioned
        if (deleteDirectory)
        {
            dirInfo.Delete();
        }
    }

Added it so you can keep your original folder

Jmyster
  • 965
  • 2
  • 9
  • 27