Link to home
Start Free TrialLog in
Avatar of jazzIIIlove
jazzIIIloveFlag for Sweden

asked on

Problem with deleting directories under C#

Hi there;

I got a question that in VS. 2008 i wrote a code to delete desktop and d:\ drive...

The problem is i want to exclude some dirs...For example "Recycler" for D: drive...

String[] dirs= Directory.GetDirectories("d:\\RECYCLER"); i have to use...But it fails...
Then iterate under for and delete the dirs if the dirname is not Recycler...That's it but i failed doing that no file/folder is deleted when i try for that...

I want to modify the below code that when the program is executed, the RECYCLER is preserved and not deleted...Other than that will be deleted...

Best regards...


internal void DeleteContentDirectories()
        {
            try
            {
                //Console.WriteLine("asdf" + _currentUserDirectoryProvider.UserDirectory + "1234");
                Directory.Delete("C:\\Documents and Settings\\" + _currentUserDirectoryProvider.UserDirectory + "\\Desktop",true);
               //Directory.CreateDirectory("C:\\Documents and Settings\\" + _currentUserDirectoryProvider.UserDirectory + "\\Desktop");
                //Directory.Delete("f:", true);
                //Directory.Delete("C:\\Documents and Settings\\" + _currentUserDirectoryProvider.UserDirectory + "\\Desktop", true);
                Directory.Delete("d:", true);
                 //+ _currentUserDirectoryProvider.UserDirectory);
            }
            catch (Exception e)
            {
            }
        }

Open in new window

SOLUTION
Avatar of MarkMyburgh
MarkMyburgh
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jazzIIIlove

ASKER

Hi there;
Thank you for your snippet...

the thing is the program deletes the CONTENT of desktop and D: drive...A cleaner program that deletes the students things...I have no problem with deleting D: but for desktop, i have some...When i delete the desktop in a recursive manner, i delete not only the content but the desktop folder itself...I don't want this...just deleting the things in desktop except recycler, my computer (I cannot find them in C:\Doc.Set\stu_id\Desktop)
I can get the stu_id without a glitch so that i can reach his/her desktop...But when i say delete it recursively, he deletes not only content but also desktop folder itself...I want it delete inside of it/content and exclude My Computer and Recycler...(What are their paths is another question here)

Thanks for your interest...

P.S. You may say delete the desktop, you will recreate it...Well no, because the machines are under domain and it has own rules...When i say recreate desktop, the program finishes the policy set to the machine...
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi there;

Thanks for your effort...To clarify:

<<hmm do you need to delete the files and the diretory

i want to delete the files and directory under desktop except for mycomputer and recycle bin...The desktop directory must stay only with recyclebin and mycomputer, other files and dirs must be purged inside desktop dir...

Also, i am deleting the D: drive (but here not a issue, i can delete d: drive content cooly)...

Best regards...
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
again a nice effort but i am reluctant to write new struct or class to handle this problem of mine...Because timing is very important in this program...The program must finish within 15 seconds at most...So, adding a struct will be a bottleneck as i presume...

Best regards...
Hi there;

by the way, i cannot find the path of RecycleBin and MyComputer under Doc. Sett.

I asked the question but i don't know how to adapt the following information to my code...

https://www.experts-exchange.com/questions/23859566/What-is-the-path-of-MyComputer-and-RecycleBin.html

Best regards...and thanks for your patience and effort...
I used your code for a unit test and works perfectly fast indeed...So struct is not that bad solution for me...Thanks of your very effort and sorry for my delay...I was away...