Delete a directory
-
Short and skinny version. I am creating a application that will copy files from one location to another. There will be an option that allows the user to automatically delete the destination folder after a specified number of days. Before implementing this functionality I have been trying to delete the directory by hard coding the destination path. But each time I do only the files are deleted and the folders are left behind and I'm left with a IOException, telling me that access to the specified path is denied. Any thoughts?
Freedom is the right to say that 2+2=5 if this is so everything else will follow.
-
Short and skinny version. I am creating a application that will copy files from one location to another. There will be an option that allows the user to automatically delete the destination folder after a specified number of days. Before implementing this functionality I have been trying to delete the directory by hard coding the destination path. But each time I do only the files are deleted and the folders are left behind and I'm left with a IOException, telling me that access to the specified path is denied. Any thoughts?
Freedom is the right to say that 2+2=5 if this is so everything else will follow.
There is an overloaded .Delete method that allows you to delete all subdirectories as well.
-
Short and skinny version. I am creating a application that will copy files from one location to another. There will be an option that allows the user to automatically delete the destination folder after a specified number of days. Before implementing this functionality I have been trying to delete the directory by hard coding the destination path. But each time I do only the files are deleted and the folders are left behind and I'm left with a IOException, telling me that access to the specified path is denied. Any thoughts?
Freedom is the right to say that 2+2=5 if this is so everything else will follow.
Hi, Try to delete the folder using "DirectoryInfo.Delete(true)" function. Please note that you need to provide the parameter as "true" so that even if there are files and sub-folder in that particular folder, they all will get deleted. Manoj
-
There is an overloaded .Delete method that allows you to delete all subdirectories as well.
I have set the recursive to true: DirectoryInfo asdf = new DirectoryInfo(temp.getDestination()); try { asdf.Delete(true); } catch(IOException d) { MessageBox.Show(d.ToString()); } The folder that I am trying to delete contains a folder, which contains another folder, which contains another folder, which contains 12 files. The 12 files get deleted but the folder that contains the 12 files is the folder that the exception says access is denied on.
Freedom is the right to say that 2+2=5 if this is so everything else will follow.
-
Hi, Try to delete the folder using "DirectoryInfo.Delete(true)" function. Please note that you need to provide the parameter as "true" so that even if there are files and sub-folder in that particular folder, they all will get deleted. Manoj
I have tried this. Thanks for the suggestion though. I just don't get why something so simple is causing me so much hassle. The folders that I am trying to delete have been copied by my application.
Freedom is the right to say that 2+2=5 if this is so everything else will follow.
-
I have tried this. Thanks for the suggestion though. I just don't get why something so simple is causing me so much hassle. The folders that I am trying to delete have been copied by my application.
Freedom is the right to say that 2+2=5 if this is so everything else will follow.
-
Got it! Nothing to do with open files or folders. When I was copying form on the source to the destination I needed to specify Access Control: if (!Directory.Exists(Dst)) { Directory.CreateDirectory(Dst); System.Security.AccessControl.DirectorySecurity asdf = new System.Security.AccessControl.DirectorySecurity(Dst, System.Security.AccessControl.AccessControlSections.All); }
Freedom is the right to say that 2+2=5 if this is so everything else will follow.
-
Short and skinny version. I am creating a application that will copy files from one location to another. There will be an option that allows the user to automatically delete the destination folder after a specified number of days. Before implementing this functionality I have been trying to delete the directory by hard coding the destination path. But each time I do only the files are deleted and the folders are left behind and I'm left with a IOException, telling me that access to the specified path is denied. Any thoughts?
Freedom is the right to say that 2+2=5 if this is so everything else will follow.
If
MicealG wrote:
access to the specified path is denied
then are you sure that the directories/files aren't Read Only or being used by another application? That's always the first thing that comes to my mind whenever something tells me access to a file is denied.
-
Got it! Nothing to do with open files or folders. When I was copying form on the source to the destination I needed to specify Access Control: if (!Directory.Exists(Dst)) { Directory.CreateDirectory(Dst); System.Security.AccessControl.DirectorySecurity asdf = new System.Security.AccessControl.DirectorySecurity(Dst, System.Security.AccessControl.AccessControlSections.All); }
Freedom is the right to say that 2+2=5 if this is so everything else will follow.