【问题标题】:How can I move into another folder to access other files in C#如何移动到另一个文件夹以访问 C# 中的其他文件
【发布时间】:2021-10-03 11:48:01
【问题描述】:

所以在我的代码中,我正在编辑文件夹中的 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();
                }
        }
    }

【问题讨论】:

    标签: c# directory filesystems openxml


    【解决方案1】:

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

    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();
            }
        }
    

    【讨论】:

    • 非常感谢您的反馈!我真的很感激。
    猜你喜欢
    • 1970-01-01
    • 2013-11-11
    • 2021-08-28
    • 2019-10-27
    • 2022-01-09
    • 1970-01-01
    • 2013-12-08
    • 2010-09-08
    相关资源
    最近更新 更多