如何移动到另一个文件夹以访问 C# 中的其他文件

How can I move into another folder to access other files in C#

所以在我的代码中,我正在从一个文件夹中编辑 word 文档,并将这些文件复制到一个临时文件夹中,我只是想知道如何访问临时文件夹中的这些文件以进行进一步的操作。这是我的代码:

 private static void editFieldsTest(string filename)
    {
        if (filename.StartsWith("q") | filename.Contains("cover"))
        {
            Console.WriteLine("\nMoving question files to temp directory\n");

            var destinationFolder = @"temp\";

            var tmp_filename = $"{destinationFolder}{ Path.GetFileName(filename)}";

            try
            {
                File.Copy(filename, tmp_filename, true);

            }
            catch (IOException iox)
            {
                Console.WriteLine(iox.Message);
            }

            int fileLen = filename.Length;
                Console.WriteLine("editing question files...\n");
                string typeOfQuestion = filename.Substring(1, fileLen - 14);
                string timeGiven = filename.Substring(3, fileLen - 14);
                string marks = filename.Substring(5, fileLen - 14);
                string learningOutcome = filename.Substring(7, fileLen - 14);
                //string fifthIndex = filename.Substring(9, fileLen - 9);

                var valuesToFill = new Content(
                    new FieldContent("qnumber", typeOfQuestion),
                    new FieldContent("qmark", marks),
                    new FieldContent("qtime", timeGiven),
                    new FieldContent("learningOutcome", learningOutcome));

                using (var outputDocument = new TemplateProcessor(tmp_filename)
                    .SetRemoveContentControls(true))
                {
                    outputDocument.FillContent(valuesToFill);
                    outputDocument.SaveChanges();
                }
        }
    }

我认为您需要的是 DirectoryInfo class,根据您的代码,您可以像这样打开所需的目录

var directory = new DirectoryInfo(destinationFolder);

然后,我想,像那样获取文件可枚举集合

var files = directory.EnumerateFiles();

MSDN

有进一步阅读的参考

另外,我可以对您的代码提出一些改进建议!

private static void editFieldsTest(string filename)
    {
        //By returning early you're reducing nesting and making your code more readable. 
        //Fail fast!
        if (!(filename.StartsWith("q") | filename.Contains("cover"))) 
            return;
        
        Console.WriteLine("\nMoving question files to temp directory\n");

        //Make things const and move them our from your method 
        //But only if you really think that they won't change
        //I suggest moving that const to method signature to improve extensibility
        const string destinationFolder = @"temp\";

        var tmpFilename = $"{destinationFolder}{ Path.GetFileName(filename)}";

        try
        {
            File.Copy(filename, tmpFilename, true);

        }
        catch (IOException iox)
        {
            Console.WriteLine(iox.Message);
        }

        //You can use var to redeem yourself from writing whole type every time
        var fileLen = filename.Length;
        Console.WriteLine("editing question files...\n");
        var typeOfQuestion = filename.Substring(1, fileLen - 14);
        var timeGiven = filename.Substring(3, fileLen - 14);
        var marks = filename.Substring(5, fileLen - 14);
        var learningOutcome = filename.Substring(7, fileLen - 14);
        //string fifthIndex = filename.Substring(9, fileLen - 9);

        var valuesToFill = new Content(
            new FieldContent("qnumber", typeOfQuestion),
            new FieldContent("qmark", marks),
            new FieldContent("qtime", timeGiven),
            new FieldContent("learningOutcome", learningOutcome));

        using (var outputDocument = new TemplateProcessor(tmpFilename)
            .SetRemoveContentControls(true))
        {
            outputDocument.FillContent(valuesToFill);
            outputDocument.SaveChanges();
        }
    }