Click here to Skip to main content
15,886,085 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to get all this files in the listbox but i dont know where put this code

C#
private void CleanTemporaryFolders()
{
    String tempFolder = Environment.ExpandEnvironmentVariables("%TEMP%");
    String recent = Environment.ExpandEnvironmentVariables("%USERPROFILE%") + "\\Recent";
    String prefetch = Environment.ExpandEnvironmentVariables("%SYSTEMROOT%") + "\\Prefetch";
    EmptyFolderContents(tempFolder);
    EmptyFolderContents(recent);
    EmptyFolderContents(prefetch);
}

private void EmptyFolderContents(string folderName)
{
    foreach (var folder in Directory.GetDirectories(folderName))
    {
        try
        {
            Directory.Delete(folder, true);
        }
        catch (Exception excep)
        {
            System.Diagnostics.Debug.WriteLine(excep);
        }
    }
    foreach (var file in Directory.GetFiles(folderName))
    {
        try
        {
            File.Delete(file);
        }
        catch (Exception excep)
        {
            System.Diagnostics.Debug.WriteLine(excep);
        }
    }
}
Posted
Updated 19-Nov-14 21:30pm
v4
Comments
Tomas Takac 20-Nov-14 5:09am    
What exactly do you want to fill in the listbox? The list of files & directories?
Srikanth59 21-Nov-14 4:59am    
i want to put all the files in the listbox
BillWoodruff 20-Nov-14 6:25am    
I have to say that your code makes me "nervous:" unless you are running your program as Admin, or have elevated your privileges in code, you are not going to access the 'PreFetch folder.

And, I have a general gut-level response to any code that does a lot of folder/file deletes without the user approving them all, having the opportunity to cancel some/all of the delete targets.

I'd like to ask you why you have designed your program this way.

Have you considered the idea of putting all the delete target filepaths into something like a CheckedListBox, and asking the user to confirm before they delete ?

You should not be trying to delete the directories before you have deleted all the files in them. You probably need a recursive method that does something like:
Delete Method
Begin
    For Each Item in DirectoryPath
    Begin
        If Item is Directory
        Then
            Call Delete (DirectoryPath + Directory)
            Delete Directory
        ElseIf Item is File
        Then
            Delete File
        EndIf
    EndFor
End

If you want the items in a listbox then you need to add them as you process them.
 
Share this answer
 
v2
Comments
BillWoodruff 20-Nov-14 6:21am    
The OP's code makes me a bit "nervous:" I'm immediately thinking of how he's going to bounce-off accessing the 'PreFetch folder unless he has run as admin, or somehow elevated his privileges; or, what I perceive as the general "un-wisdom" of massive folder/file deletes.

I am curious, however: I do assume that if one did successfully delete every directory in a Folder, then what remained in the Folder could be only ... files.
Richard MacCutchan 20-Nov-14 7:13am    
One has to assume that the OP knows what he/she is doing (ha ha!).
And you cannot delete directories unless you delete all their files first.
Your question is not entirely clear, but here is how you can collect names of all the files you are deleting. However, please take the time to review all the other comments here, there is a lot of valid points you should really consider.

C#
private List<string> EmptyFolderContents(string folderName)
{
    // directories omitted as you are only interested in files
    var deletedFiles = new List<string>();
    foreach (var file in Directory.GetFiles(folderName))
    {
        try
        {
            File.Delete(file);
            deletedFiles.Add(file); // file has been deleted successfully
        }
        catch (Exception excep)
        {
            System.Diagnostics.Debug.WriteLine(excep);
        }
    }
    return deletedFiles;
}

The you need to feed the list into your list box. I assume that part is clear since you posted the code relevant to deleting the files, not the UI-related code.
 
Share this answer
 
v2
Comments
Wastedtalent 21-Nov-14 7:52am    
You could have finished it considering it's only a couple more lines?

List<string> Deleted = EmptyFolderContents(string folderName);

listBox1.DataSource = Deleted;
Tomas Takac 21-Nov-14 8:17am    
You are right that it's not complicated. But it seems too far fetched as we do not know anything about OP's UI. I even hesitated to post this answer becuase it was not clear enough that this is the problem at hand.
Wastedtalent 21-Nov-14 9:16am    
Fair enough, it did seem like a UI question with no UI code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900