Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Delete a directory

Delete a directory

Scheduled Pinned Locked Moved C#
questiondiscussionannouncement
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MicealG
    wrote on last edited by
    #1

    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.

    P M J 3 Replies Last reply
    0
    • M MicealG

      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.

      P Offline
      P Offline
      Paul Brower
      wrote on last edited by
      #2

      There is an overloaded .Delete method that allows you to delete all subdirectories as well.

      M 1 Reply Last reply
      0
      • M MicealG

        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.

        M Offline
        M Offline
        Manoj Kumar Rai
        wrote on last edited by
        #3

        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

        M 1 Reply Last reply
        0
        • P Paul Brower

          There is an overloaded .Delete method that allows you to delete all subdirectories as well.

          M Offline
          M Offline
          MicealG
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • M Manoj Kumar Rai

            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

            M Offline
            M Offline
            MicealG
            wrote on last edited by
            #5

            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.

            J 1 Reply Last reply
            0
            • M MicealG

              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.

              J Offline
              J Offline
              jayart
              wrote on last edited by
              #6

              Try checking if any of the files in the folder are left open.

              M 1 Reply Last reply
              0
              • J jayart

                Try checking if any of the files in the folder are left open.

                M Offline
                M Offline
                MicealG
                wrote on last edited by
                #7

                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.

                M 1 Reply Last reply
                0
                • M MicealG

                  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.

                  J Offline
                  J Offline
                  Jimmanuel
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  • M MicealG

                    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.

                    M Offline
                    M Offline
                    MicealG
                    wrote on last edited by
                    #9

                    Thanks to everyone for their suggestions. They were greatly appreciated .

                    Freedom is the right to say that 2+2=5 if this is so everything else will follow.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups