简体   繁体   English

当Directory.Move子文件夹和父文件夹时,IOException访问被拒绝

[英]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. 我试图重命名具有子文件夹的多个文件夹,但是当我在子文件夹上使用Directory.Move()时,父文件夹似乎被锁定了。

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 最后一个Directory.Move( )抛出一个

IOException: Access to "D:\\test\\f1" is denied IOException:拒绝访问“ D:\\ test \\ f1”

Does anyone know how I can rename both folders? 有谁知道我可以重命名两个文件夹吗?

I figured out what the problem was. 我发现了问题所在。 I had to close File Explorer in Windows. 我必须在Windows中关闭文件资源管理器。 The process was locking up the folders somehow. 该过程以某种方式锁定了文件夹。

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. 您需要先关闭该文件流,然后才能重命名该文件夹。

Try this .. 尝试这个 ..

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM