2

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?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Jompa234
  • 1,228
  • 2
  • 14
  • 24

3 Answers3

3

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

Jompa234
  • 1,228
  • 2
  • 14
  • 24
0

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.

Weyland Yutani
  • 4,682
  • 1
  • 22
  • 28
0

Try this ..

 DirectoryInfo dirInfo = new DirectoryInfo(oldpath);
 dirInfo.MoveTo(newPath);
Jidheesh Rajan
  • 4,744
  • 4
  • 23
  • 29