dcsimg
Results 1 to 6 of 6

Thread: VB.net - Deleted directories remain there plus that they become inaccessible

Hybrid View

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2018
    Posts
    428

    VB.net - Deleted directories remain there plus that they become inaccessible

    Hi
    I am creating and deleting some directories via code. Creating is fine, but when I delete them, the the code runs fine (without error) but the directories are still there in Windows explorer (even after refreshing and stuff). When I try to open them, i get a permission denied and you need to be admin etc. Deleting them manually gives the same error.

    Any idea what is going on here?

    code:
    Code:
        
     ' check if the folders needs to be deleted too
                If RemoveFolder = True Then ' delete folders
    
                    Dim DestinationRoot As String = GetScalarValue("SELECT ConfigValue FROM SystemConfiguration WHERE ConfigItem ='Mentor Files'", "Getting the root directory")
    
                    Try
                        System.IO.Directory.Delete(DestinationRoot & "\" & FoldersToDelete, True)
                    Catch ex As Exception
                        MsgBox(ex.Message, MsgBoxStyle.Critical, "Error when deleting folder: " & vbCrLf & vbCrLf & DestinationRoot & "\" & FoldersToDelete)
                        Exit Sub
                    End Try
    
                End If
    Thanks for help.

    Edit:
    I forgot to mention that I can create adn delete folders without problem. I also can manually delete folders that are created through the code.
    Last edited by Grand; Dec 24th, 2019 at 11:35 AM.

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,804

    Re: VB.net - Deleted directories remain there plus that they become inaccessible

    Quote Originally Posted by Grand View Post
    Hi
    I am creating and deleting some directories via code. Creating is fine, but when I delete them, the the code runs fine (without error) but the directories are still there in Windows explorer (even after refreshing and stuff). When I try to open them, i get a permission denied and you need to be admin etc. Deleting them manually gives the same error.

    Any idea what is going on here?

    code:
    Code:
        
     ' check if the folders needs to be deleted too
                If RemoveFolder = True Then ' delete folders
    
                    Dim DestinationRoot As String = GetScalarValue("SELECT ConfigValue FROM SystemConfiguration WHERE ConfigItem ='Mentor Files'", "Getting the root directory")
    
                    Try
                        System.IO.Directory.Delete(DestinationRoot & "\" & FoldersToDelete, True)
                    Catch ex As Exception
                        MsgBox(ex.Message, MsgBoxStyle.Critical, "Error when deleting folder: " & vbCrLf & vbCrLf & DestinationRoot & "\" & FoldersToDelete)
                        Exit Sub
                    End Try
    
                End If
    Thanks for help.

    Edit:
    I forgot to mention that I can create adn delete folders without problem. I also can manually delete folders that are created through the code.
    ???? In one statement you say you CAN'T delete them manually:
    Deleting them manually gives the same error.
    And then later you say you can:
    I also can manually delete folders that are created through the code.
    So.... yeah...

    But, that said... it sounds like you're running into a Windows File Permissions issue... depending on where the folders you're trying to delete are, that's what I'd expect.

    On a side and un-related note... I wouldn't use this:
    Code:
    DestinationRoot & "\" & FoldersToDelete
    To combine paths.... Instead, use the System.IO.Path.Combine method
    Code:
    System.IO.Path.Combine(DestinationRoot, FoldersToDelete)
    And since I'm feeling a bit pedantic, I'd rename FoldersToDelete to FolderToDelete singluar since it is just a folder not a group of folders, or a list or multiple folders in any way... it's a singular folder.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2018
    Posts
    428

    Re: VB.net - Deleted directories remain there plus that they become inaccessible

    OK, Thanks for the info and cleaning up tips, it makes sense.

    I guess my statements are not chronologically structured.
    Before they are "deleted" at run time, I can delete them without any error. However, after the code presumably deleted them (they are still visible) then they cannot be deleted; even when I try to do it manually; permission things comes up.

    I am just wondering if there is any permission issue why can I then create and delete manually?

    Edit:
    Aha, I just checked and they are gone. Why are they not removed immediately when deleted via code? Am I triggering anything with that code?
    Last edited by Grand; Dec 25th, 2019 at 10:08 AM.

  4. #4
    Member Grant Swinger's Avatar
    Join Date
    Jul 2015
    Posts
    59

    Re: VB.net - Deleted directories remain there plus that they become inaccessible

    I've seen something like this before when I was deleting and then recreating a directory. After some experimentation I decided that when Directory.Delete returns without an error it hasn't actually deleted the directory. It seems to be doing a quick check and if nothing seems wrong (such as permissions) it marks the directory for deletion, passes the job off to Windows, and then reports success even though the directory won't be gone until Windows gets around to it.

    You might try calling Directory.Exists afterwards. Checking for a directory that's marked for deletion seemed to force Windows to actually get the deletion done.

  5. #5
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,923

    Re: VB.net - Deleted directories remain there plus that they become inaccessible

    In the "old days", the deletion of the directories and folders would be done in line with the code, but this slows the code down quite a bit.
    So "modern" systems do many of the slow file operations asynchronously so that your code isn't impacted by the relative slowness of file operations, i.e. caching reads and writes, etc...
    Your code could be doing many thousands of things in the time it takes to access the file systems structure and modify the various areas of the disk necessary to remove files and directories.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2018
    Posts
    428

    Re: VB.net - Deleted directories remain there plus that they become inaccessible

    Thanks both for the explanation. I will check if the directory exist after "deletion". Also I will try to create a directory with the same name after the deletion just to check things out.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width