I am trying to develop a junk file cleaner that will take multiple directories using the checked property of checkboxes.There are many checkboxes in the form such as temp,search history,etc..
i am using following code for deletion :

string[] filePaths = Directory.GetDirectories(@"C:\folder");
 foreach (string filePath in filePaths)
 Directory.Delete(filePath, true);

I dont want to specify the path here instead i want to specify it somewhere else.To keep the code portable i want to run the delete function only once to delete all the paths.
how should i do it?

Edited 5 Years Ago by __avd: Added [code] tags.

Could you please explain from where do u want to specify the path. If you want to specify them at particular instance like textbox then u can give that textbox id in the place of ur path in the above code.

Could you please explain from where do u want to specify the path. If you want to specify them at particular instance like textbox then u can give that textbox id in the place of ur path in the above code.

Hello munnazz,
There are atleast 10 different paths from where i have to delete files and folders.
Couple of paths are
C:/Windows/temp
C:/Recycled
I want to delete files from both "temp" & "Recycled" folders using one deletion code, this way code will be more portable.

public void DeleteDirectories(String[] directories) {
    foreach (String dir in directories) {
        Directory.Delete(dir, true); 
    }
}

Code will throw exceptions if it encounters problems which means some directories may have been deleted and others not.

public void DeleteDirectories(String[] directories) {
    foreach (String dir in directories) {
        Directory.Delete(dir, true); 
    }
}

Code will throw exceptions if it encounters problems which means some directories may have been deleted and others not.

Thanks for the reply Momerath,
I understood how this method is working, but please could you tell me how i can provide this method 5-10 different locations to delete at the same time.......??

DeleteDirectories(new String[] {@"C:\temp", @"C:\myOtherdirectory"});

// or
string[] myStrings = {"C:\\temp", "C:\\myOtherdirectory"};
DeleteDirectories(myStrings);

For "portability", if you plan to add directories later on, you could put them in a file that you can change without the need to re-build.

void function()
{
	String dirlist = File.ReadAllText("ToDelete.txt");
	char[] nl = Environment.NewLine.ToCharArray();
	String[] dirs = dirlist.Split(nl);
	DeleteDirectories(dirs);
}

public void DeleteDirectories(String[] directories)
{
	foreach (String dir in directories)
	{
		Directory.Delete(dir, true);
	}
}

In your ToDelete.txt file:

C:/Windows/temp
C:/Recycled
another dir...

Edited 5 Years Ago by makman99: n/a

i want to develop an ap like ccleaner,if a particular checkbox is selected then the path is deleted else not deleted.if i give a list of paths in a file all will be deleted at once even if the checkboxes aren't selected by the user.

For "portability", if you plan to add directories later on, you could put them in a file that you can change without the need to re-build.

void function()
{
	String dirlist = File.ReadAllText("ToDelete.txt");
	char[] nl = Environment.NewLine.ToCharArray();
	String[] dirs = dirlist.Split(nl);
	DeleteDirectories(dirs);
}

public void DeleteDirectories(String[] directories)
{
	foreach (String dir in directories)
	{
		Directory.Delete(dir, true);
	}
}

In your ToDelete.txt file:

C:/Windows/temp
C:/Recycled
another dir...

i want to develop an application like ccleaner,if a particular checkbox is selected then the path is deleted else not deleted.if i give a list of paths in a file all will be deleted at once even if the checkboxes aren't selected by the user.

String[] dirs = dirlist.Split(nl);
DeleteDirectories(dirs);

When you get here, instead of directly calling DeleteDirectories(), add the array of dirs into a list of checkboxes and then make a button that, when clicked, checks for all the dirs checked and add those to a new array and then call DeleteDirectories()

Keep in mind that the system protects some files so you have to pay close attention to any errors. try using as try catch block so that you know what errors are being thrown. here is a simple method that i use to clear the internet cache in vista

using System;
using System.IO;


    public class InternetCacheCleaner
    {
       public static void clearIECache()
        {
            ClearFolder(new DirectoryInfo(Environment.GetFolderPath
               (Environment.SpecialFolder.InternetCache)));
        }

        private static void ClearFolder(DirectoryInfo folder)
        {
            foreach (FileInfo file in folder.GetFiles())
            {
                if (file.Name != "index.dat")
                {
                    file.Delete();
                }
            }
            foreach (DirectoryInfo subfolder in folder.GetDirectories())
            { ClearFolder(subfolder); }
        }
    }
String[] dirs = dirlist.Split(nl);
DeleteDirectories(dirs);

When you get here, instead of directly calling DeleteDirectories(), add the array of dirs into a list of checkboxes and then make a button that, when clicked, checks for all the dirs checked and add those to a new array and then call DeleteDirectories()

Cant i get this kind of code in csharp

this is the code in java

class DelDir{
//deletion code
}

private void CleanMouseClicked(java.awt.event.MouseEvent evt) {

DelDir dir=new DelDir();
if(temp.isSelected()){
dir.deleteDirectory(new File("C:\\WINDOWS\\temp"));

}
if(logfiles.isSelected()){
dir.deleteDirectory(new File("C:\\WINDOWS\\system32\\wbem\\Logs"));
}
}

here is a short example try this let me know how it works for you

DirectoryInfo info = new DirectoryInfo("C:\\Windows\\Temp");
                foreach (FileSystemInfo inf in info.GetFiles())
                {
                    File.Delete(inf.FullName);
                }

Note this only deletes files from top directory!! does not delete files from subfolders or directorys within top directory For a more elaborate method check out my earlier post

Edited 5 Years Ago by CsharpChico: n/a

public void DeleteDirectories(String[] directories) {
    foreach (String dir in directories) {
        Directory.Delete(dir, true); 
    }
}

Code will throw exceptions if it encounters problems which means some directories may have been deleted and others not.

You do not want to Delete the Directory Windows\Temp! You Want to use DirectoryInfo to obtain directories within the base directory

here is a short example try this let me know how it works for you

DirectoryInfo info = new DirectoryInfo("C:\\Windows\\Temp");
                foreach (FileSystemInfo inf in info.GetFiles())
                {
                    File.Delete(inf.FullName);
                }

Note this only deletes files from top directory!! does not delete files from subfolders or directorys within top directory For a more elaborate method check out my earlier post

Can i declare "C:\\Windows\\Temp" path within checkbox scope so that if it is selected it will send the path to "DirectoryInfo" function.

This article has been dead for over six months. Start a new discussion instead.