0

In vs2010, there is a vb.net project, I need to clean a folder before use it, what I did is remote the folder, then create it again. but the problem is ,it is removed but not created again.

I think maybe .net did some optimization. So how can I fix that?

Here is the code:

    ' delete folder
    If IO.Directory.Exists(exportBaseFolder) = True Then
        IO.Directory.Delete(exportBaseFolder, True)
    End If

    ' create folder
    IO.Directory.CreateDirectory(exportBaseFolder)
6
  • If the folder has any substantial files to it, the delete process is still going on while your trying to create it. Commented Apr 9, 2015 at 4:14
  • @OneFineDay Ok, so how do I fix that? Commented Apr 9, 2015 at 4:23
  • One way is with an empty while loop that exits when the directory no longer exists. Then create it. Commented Apr 9, 2015 at 4:39
  • @tinstaafl That seems not very good... Commented Apr 9, 2015 at 4:43
  • 2
    No, but then deleting a directory and all files and subdirectories just to create and empty one is an unusual situation. You could rename the directory then delete it. While it's deleting you can create the new one. Commented Apr 9, 2015 at 4:49

1 Answer 1

1

You could try Directory.GetFiles and foreach file, file.delete. Then delete the folder. Then if not folder exists, create it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.