1

My question is identical to: How to rename a folder in c# which is currently opened by windows explorer

The author said that his solution was to use SHFileOperation() but it should be a pain in the a.. which is also my experience.

Interestingly, a user has since then posted that using a reference to Visual Basic and the command Microsoft.VisualBasic.FileIO.FileSystem.MoveDirectory(...) works even though the folder is currently opened by windows explorer.

However, it seems like the Visual Basic command is not atomic in the same way as the C# commands Directory.Move / DirectoryInfo.MoveTo.

If you try to move a folder with the C# commands, and some of the files in the folder are currently locked, then you'll get an exception and C# won't move anything. Like it's an atomic operation and it will either move everything or nothing.

Using the Visual Basic command, you'll still get an exception if some files in the folder are locked, however, it will move the files which aren't locked and leave the remaining things including the main folder.

What I need is a command that can move a folder even though a user is standing in that folder in windows explorer. If a file in the folder is locked, then throw an exception and don't move anything.

Is there a way to make the Visual Basic command atomic? Like a stop method if it throws an exception so it won't move anything?

Community
  • 1
  • 1
Jacob S
  • 11
  • 3

0 Answers0