c# - C# : File. Exists 中的 8 个条件的模式匹配返回 false,但文件确实存在

标签 c#

从下面考虑该程序。其思路如下:在 Main 方法中,调用 AreAllArgumentsPassedAndValid() 来检查某些参数的正确性。该检查是使用所谓的元组匹配来进行的。总共应检查 7 + 1 个条件。为了简单起见,我将其中 7 个条件替换为具有永久赋值的 bool 变量,因为我确信它们会被正确检查。我只保留了原始程序中出现的最后一个条件。在最后一个条件中,将检查字符串是否不为空或零,然后检查文件是否存在(因此字符串代表路径)。

问题:我发现如果我的开关中至少有 8 个条件,则永远找不到该文件。但是:一旦我从开关中删除一个条件,例如 cond7,我总共只有 7 个条件,在这种情况下,会正确搜索文件并返回正确的值,具体取决于文件是否存在。

我将文件放在哪里并不重要 - 我尝试将文件直接放在 C:\ 下。那里也没有找到。但是,如果我单独调用 File.Exists ,而不是在返回/开关中,则文件搜索可以正常工作。

我的问题如下:return/switch 中的参数数量对 File.Exists(oFileName) 的正确工作有什么影响?当开关中有 8 个条件时,我尝试调试 File.Exists(oFileName) 方法,但这并没有让我找到解决方案。

有关我的电脑的信息:所有电脑都有 Win 10 x64 和相同的 .NET 版本。所有这些问题都是相同的。

整个程序在return/switch中有8个条件:

using System;
using System.IO;

namespace PatternMatchingTest
{
    class Program
    {
        private static class Constants
        {
            public const string ProgramSide = "PROGRAM_SIDE";
            public const string OldFileName = "OLD_FILE_NAME";
            public const string NewFileName = "NEW_FILE_NAME";
        }
        
        static void Main(string[] args)
        {
            string oldFilePath = @"C:\Users\ADMINI~1\AppData\Local\Temp\Test.txt";
            
            Console.WriteLine(AreAllArgumentsPassedAndValid());
            Console.ReadKey();
            
            string AreAllArgumentsPassedAndValid()
            {
                string oFileName = oldFilePath;
                bool cond1 = true, cond2 = true, cond3 = true, cond4 = true, cond5 = true, cond6 = false, cond7 = false;
                return (cond1,
                        cond2,
                        cond3,
                        cond4,
                        cond5,
                        cond6,
                        cond7,
                        !string.IsNullOrEmpty(oFileName) && File.Exists(oFileName))
                    switch
                    {
                        (false, _, _, _, _, _, _, _) => $"Invalid number of arguments. 3 arguments are expected.",
                        (_, false, _, _, _, _, _, _) => $"Missing {Constants.ProgramSide} argument.",
                        (_, _, false, _, _, _, _, _) => $"Missing {Constants.OldFileName} argument.",
                        (_, _, _, false, _, _, _, _) => $"Missing {Constants.NewFileName} argument.",
                        (_, _, _, _, false, _, _, _) => $"Argument {Constants.ProgramSide} has invalid value.",
                        (_, _, _, _, _, true, _, _) => $"Argument {Constants.OldFileName} has invalid value: null or empty. Expected: path to a file.",
                        (_, _, _, _, _, _, true, _) => $"Argument {Constants.NewFileName} has invalid value: null or empty. Expected: path to a file.",
                        (_, _, _, _, _, _, _, false) => $"File {oFileName} does not exist.",
                        (true, true, true, true, true, false, false, true) => string.Empty
                    };
            }
        }
    }
}

AreAllArgumentsPassedAndValid 方法只有 7 个条件 - 在这种情况下,程序按预期工作:

string AreAllArgumentsPassedAndValid()
{
    string oFileName = oldFilePath;
    bool cond1 = true, cond2 = true, cond3 = true, cond4 = true, cond5 = true, cond6 = false, cond7 = false;
        return (cond1,
                cond2,
                cond3,
                cond4,
                cond5,
                cond6,

                !string.IsNullOrEmpty(oFileName) && File.Exists(oFileName))
                switch
                {
                    (false, _, _, _, _, _, _) => $"Invalid number of arguments. 3 arguments are expected.",
                    (_, false, _, _, _, _, _) => $"Missing {Constants.ProgramSide} argument.",
                    (_, _, false, _, _, _, _) => $"Missing {Constants.OldFileName} argument.",
                    (_, _, _, false, _, _, _) => $"Missing {Constants.NewFileName} argument.",
                    (_, _, _, _, false, _, _) => $"Argument {Constants.ProgramSide} has invalid value.",
                    (_, _, _, _, _, true, _) => $"Argument {Constants.OldFileName} has invalid value: null or empty. Expected: path to a file.",
                    (_, _, _, _, _, _, false) => $"File {oFileName} does not exist.",
                    (true, true, true, true, true, false,  true) => string.Empty
                };
}

最佳答案

当创建一个包含 8 个值的元组时,它 starts to get weird 。更好的方法是创建一个具有命名属性的对象,因为我认为您拥有的元组是无法读取的。

关于c# - C# : File. Exists 中的 8 个条件的模式匹配返回 false,但文件确实存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63346305/

相关文章:

c# - 如何获得linq中最高价和最低价商品的数量之和

c# - HTTP 状态 403 : Forbidden exception using certificate to authenticate ASP. NET Web 服务

c#: 初始化一个 DateTime 数组

c# - 将 C# 程序移动到不同的语言

c# - 使用 IMiddleware 时添加自定义中间件不起作用

c# - C# 控制台应用程序可以接收空参数吗?

c# - WebAPI 接受不同的整数值

c# - DDD : Trying to code sorting and filtering as it pertains to Poco, 使用 C# 的存储库、DTO 和 DAO?

c# - 需要 HttpWebRequest 对象的帮助

c# - 创建在 for/foreach 循环中使用的对象的最佳实践