IOException access denied when Directory.Move subfolder and parent folder

試著忘記壹切 提交于 2019-12-11 05:45:23

问题


I am trying to rename multiple folders that have subfolders, but when I use Directory.Move() on a subfolder the parent folder seem to get locked up.

My code:

var pathParent = @"D:\test\f1";
var pathSub = @"D:\test\f1\f2";

var pathParentnew = @"D:\test\f1new";
var pathSubnew = @"D:\test\f1\f2new";

Directory.Move(pathSub, pathSubnew);
Directory.Move(pathParent, pathParentnew);

The last Directory.Move() throws an

IOException: Access to "D:\test\f1" is denied

Does anyone know how I can rename both folders?


回答1:


I figured out what the problem was. I had to close File Explorer in Windows. The process was locking up the folders somehow.




回答2:


Your program has previously opened a file in the parent directory. You need to close that file stream before it will let you rename the folder.




回答3:


Try this ..

 DirectoryInfo dirInfo = new DirectoryInfo(oldpath);
 dirInfo.MoveTo(newPath);


来源:https://stackoverflow.com/questions/19002085/ioexception-access-denied-when-directory-move-subfolder-and-parent-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!