所以我正在重新编写一个例程来重命名东西。我的文件夹树结构与此类似:
Folder -> Folder(s) -> Folder(s) + ImageFile(s)
Session -> Location(s) -> Test(s) + Images
所有位置都有相同的测试,重命名一个必须重命名所有。此外,每个图像都有一个相关的文件夹,名称相同的相关文件夹。因此图像“一”、“二”、“三”将在同一目录(位置)中同时具有文件夹“一”、“二”、“三”。
我正在使用此代码重命名:
DirectoryInfo MainSessionFolder = new DirectoryInfo(FileStructure.CurrentSessionPath); // Main Session Folder
DirectoryInfo[] locations = MainSessionFolder.GetDirectories(); // Get all locations
foreach(var location in locations) // for every location
{
DirectoryInfo[] TestFolders = location.GetDirectories(); // get test folders in that location
foreach (var TestFolder in TestFolders) // for every test folder
{
if(SelectedTest.Name == TestFolder.Name) // check it it's what we want to rename
Directory.Move(TestFolder.FullName, TestFolder.Name.Replace(SelectedTest.Name, NewName)); // rename it
}
FileInfo[] AllFiles = location.GetFiles(); // get all files in that location
foreach (var ThisFile in AllFiles) // for every file in that location
{
if (SelectedTest.Name == ThisFile.Name) // check
File.Move(SelectedTest.Name, NewName); // rename
}
}
DirectoryInfo.move
它会在(不是FileInfo.move
,很奇怪,因为错误说文件)上弹出一个错误,说:
'System.IO.IOException' 类型的异常 ....
.. 当文件已存在时无法创建文件..