Search code examples
c#ioexception.net

IOException access denied when Directory.Move subfolder and parent folder


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?


Solution

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