C# c在不同目录之间移动文件

C# c在不同目录之间移动文件,c#,C#,这是我的全部代码,它读取csv文件并输出某些数字,然后将文件移动到另一个文件夹。我需要帮助修复的是它拉入文件和移动文件的方式。我目前有两个名为Hotels和Completed Hotels的文件夹。在Hotels文件夹中,它有6个不同酒店的子文件夹。按照现在的工作方式,它将一次从整个Hotels文件夹、每个Hotels文件夹中提取所有文件,并将它们全部移动到completed Hotels文件夹中。我想更改的是在每个酒店文件夹中都有一个完整的文件夹,我想让它从酒店中提取数据并将其放入自己的完整文

这是我的全部代码,它读取csv文件并输出某些数字,然后将文件移动到另一个文件夹。我需要帮助修复的是它拉入文件和移动文件的方式。我目前有两个名为Hotels和Completed Hotels的文件夹。在Hotels文件夹中,它有6个不同酒店的子文件夹。按照现在的工作方式,它将一次从整个Hotels文件夹、每个Hotels文件夹中提取所有文件,并将它们全部移动到completed Hotels文件夹中。我想更改的是在每个酒店文件夹中都有一个完整的文件夹,我想让它从酒店中提取数据并将其放入自己的完整文件夹中,然后移动到下一家酒店并执行相同的操作,我将如何更改代码以实现这一点

代码如下:

class ManagerReport
{
    static void ProcessFilesCSVFiles(string originalPath, string destinationPath)
    {
        // first check if path exists
        if (!Directory.Exists(originalPath))
            // doesn't exist then exit, can't copy from something that doesn't exist
            return;
        var copyPathDirectory = new DirectoryInfo(originalPath);
        // using the SearchOption.AllDirectories will search sub directories
        var copyPathCSVFiles = copyPathDirectory.GetFiles("*.txt", SearchOption.AllDirectories);

        // loops through directory looking for txt files
        for (var i = 0; i < copyPathCSVFiles.Length; i++)
        {
            // get the file
            var csvFile = copyPathCSVFiles[i];

            // [...] parse and import the file

            // creates a variable that combines the the directory of the new folder with the file name
            var destinationFilePath = Path.Combine(destinationPath, csvFile.Name);
            // This loop prevents duplicates. If a file is already in the folder, it will delete the file already in there and move this one in.
            // Shouldn't be an issue since each file will have a different name
            if (File.Exists(destinationFilePath))
            {
                File.Delete(destinationFilePath);
            }
            // moves it to the new folder
            csvFile.MoveTo(destinationFilePath);
        }
    }

    static void Main(string[] args)
    {
        ProcessFilesCSVFiles(@"C:\Users\Documents\Hotels", @"C:\Users\Documents\Completed Hotels");
    }
}

因此,基本上,您有一个如下的源文件结构:

Prefix\Hotel1\File1.csv
Prefix\Hotel1\File2.csv
Prefix\Hotel2\File1.csv
Prefix\Hotel3\File3.csv
Prefix\Hotel1\Completed\File1.csv
Prefix\Hotel1\Completed\File2.csv
Prefix\Hotel2\Completed\File1.csv
Prefix\Hotel3\Completed\File3.csv
您要处理这些文件,然后将其移动到目标目录,如下所示:

Prefix\Hotel1\File1.csv
Prefix\Hotel1\File2.csv
Prefix\Hotel2\File1.csv
Prefix\Hotel3\File3.csv
Prefix\Hotel1\Completed\File1.csv
Prefix\Hotel1\Completed\File2.csv
Prefix\Hotel2\Completed\File1.csv
Prefix\Hotel3\Completed\File3.csv
当前代码使用DirectoryInfo.GetFiles,它递归扫描目录并返回完整路径。要确定哪个文件属于哪个文件夹,需要再次拆分路径并确定父文件夹

一个简单的修复方法是简单地迭代源目录,边走边处理和移动文件:

foreach (var hotelDirectory in Directory.GetDirectories(sourceDirectory))
{
    foreach (var inputFile in Directory.GetFiles(hotelDirectory))
    {
        // process inputFile

        var destinationPath = Path.Combine(hotelDirectory, "Completed");
        Directory.CreateDirectory(destinationPath);

        var destinationFileName = Path.GetFileName(inputFile);
        destinationPath = Path.Combine(destinationPath, destinationFileName);

        File.Move(inputFile, destinationPath);
    }
}

你试过什么?你被困在哪里?你能阅读并提供一个,强调最低限度吗?我甚至不知道从哪里开始,因为我觉得现在的写作方式根本不起作用。现在它正在提取它看到的所有txt文件,但我的问题是,我担心它会从已完成的文件夹中提取txt文件,以及您发布的其余代码发生了什么。。另外,我个人认为,通过创建一个类,模仿您试图读取和/或创建的csv文件的结构,然后对csv字段使用自动属性,然后创建一个列表类,您可以更轻松地完成这项工作。。然后将这些行读入类中,然后将数据另存为csv等。@MethodMan这是csv解析和数据库更新代码,与问题完全无关,因此我将其编辑掉。这很有效,谢谢。唯一的问题是我遇到了此错误:mscorlib.dll中发生了类型为“System.IO.FileNotFoundException”的未处理异常。其他信息:找不到文件“C:\Users\Documents\Hotels\HEW\HEW.txt”。此错误发生在文件.Move部分。当我检查我的文件夹时,文件已经被移动了,所以你一定留下了一些代码。我删除了最初用于移动文件的整个底部。我离开了顶部,但为什么文件已经移动了超过?我发布的代码移动了文件一次。如果它已经被移动了,还有其他代码我帮不上忙。设置断点,逐步完成代码。