How to rename a folder in c# which is currently opened by windows explorer

后端 未结 3 1139
臣服心动
臣服心动 2021-02-13 18:41

When renaming a folder in C#, System.IO.Directory.Move throws System.IO.IOException (message \"access denied\") if that folder or any subfolder is curr

3条回答
  •  广开言路
    2021-02-13 19:06

    I had the same problem. As soon as I had an explorer window open and once navigated into the folder to rename, the Directory.Move failed with a "access denied" (Windows 7 Professional 64bit, application compiled as x86).

    Interestingly, the command Microsoft.VisualBasic.FileIO.FileSystem.MoveDirectory(...) succeeds to move the contents to a new directory, it only fails to delete the old directory if you are staying inside a subfolder of the directory to move. This can be fixed by catching the exception that is thrown on the first error and try again a second time. Now the source folder is also removed.

提交回复
热议问题