问题
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