Directory.Move bug when using closing backslash?

N

ngposter

Try the following code snippet:

Directory.CreateDirectory(@"C:\Folder1\");
Directory.Move(@"C:\Folder1\", @"C:\Folder2\");
Directory.Delete(@"C:\Folder2\");

After executing, a C:\Folder1 still exists!

Stranger yet, place a breakpoint on "Directory.Delete(@"C:\Folder2\");" and
run the code. Have Windows Explorer open in the background.

At the breakpoint, you will see that (1) Folder1 is deleted and (2) Folder2
exists.
Now step through the "Directory.Delete(@"C:\Folder2\");" line.
Folder2 is deleted, but somehow Folder1 comes back!

The problem has to do with the closing backslash in the target parameter of
the .Move function.
The following will work as expected.

Directory.Move(@"C:\Folder1\", @"C:\Folder2");

Can someone confirm the same behavior?
If this is the case, I'd like to know: Is it bad practice to use a closing
backslash to differentiate folder paths from file paths?

Thanks.

btw: I have version 1.1.4322 of the framework.
 
M

Michael Culley

ngposter said:
Can someone confirm the same behavior?
If this is the case, I'd like to know: Is it bad practice to use a closing
backslash to differentiate folder paths from file paths?

You can't have a folder and file of the same name so there is no need to differentiate. Generally I wouldn't use a trailing slash
because it would indicated to me the next level eg "C:\Folder1\" seems to indicate something in folder1, not folder1 itself.
 
M

Morten Wennevik

Hm,

Rather strange. As Michael said you do not normally deal with directories using trailing \, but using one shouldn't do any harm either. The problem is with Directory.Move which not only moves a directory, but also the contents, and having a trailing \ in the destination directory confuses it. However, I have no idea how or why it manages to recreate Folder1 after removing Folder2. It would be interesting if someone could shed any light on that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top