我多次重命名目录时遇到问题,它似乎锁定了文件。
// e comes from a objectListView item
DirectoryInfo di = (DirectoryInfo)e.RowObject;
DirectoryInfo parent = Directory.GetParent(di.FullName);
String newPath = Path.Combine(parent.FullName, e.NewValue.ToString());
// rename to some temp name, to help change lower and uppercast names
di.MoveTo(newPath + "__renameTemp__");
di.MoveTo(newPath);
// Trying to cleanup to prevent directory locking, doesn't work...
di = null;
parent = null;
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
非常感谢任何帮助,因为第一次重命名工作正常,但是当尝试对重命名的文件夹进行新的重命名时,它会引发异常:
该进程无法访问该文件,因为它正被另一个进程使用。mscorlib.dll 中出现了“System.IO.IOException”类型的第一次机会异常
所以第一次重命名该文件夹有效,第二次抛出异常,我猜应用程序锁定了新文件夹,但如何解决它?我应该能够重命名一个文件夹两次,对吗?