-
Dec 24th, 2019, 11:31 AM
#1
Thread Starter
Fanatic Member
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.
-
Dec 24th, 2019, 11:48 AM
#2
Re: VB.net - Deleted directories remain there plus that they become inaccessible
Originally Posted by Grand
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
-
Dec 24th, 2019, 01:18 PM
#3
Thread Starter
Fanatic Member
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.
-
Dec 24th, 2019, 02:10 PM
#4
Lively Member
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.
-
Dec 24th, 2019, 02:42 PM
#5
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
-
Dec 25th, 2019, 10:07 AM
#6
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|