0

所以我正在重新编写一个例程来重命名东西。我的文件夹树结构与此类似:

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' 类型的异常 ....
.. 当文件已存在时无法创建文件..

4

1 回答 1

1

听起来您从和到的移动都指向同一个位置。

尝试暂时将您的 Directory.Move 语句分解为更小的部分,然后逐步检查所有值是否符合您的预期。

string target = TestFolder.Name.Replace(SelectedTest.Name, NewName);

然后检查 TestFolder.FullName、TestFolder.Name、SelectedTest.Name、NewName、target 的值

检查它们是否符合您的期望,并在资源管理器中检查目标目录尚不存在。

应该有希望让你知道出了什么问题。

于 2016-04-04T18:09:21.557 回答