Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#:File.Exists中带有8个条件的模式匹配返回false,但该文件确实存在_C# - Fatal编程技术网

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

C#:File.Exists中带有8个条件的模式匹配返回false,但该文件确实存在,c#,C#,考虑下面的程序。其思想应该如下:在Main方法中,调用ArealArgumentSpassedValid(),检查某些参数的正确性。检查是使用所谓的元组匹配来执行的。应检查总共7+1个条件。为了简单起见,我用永久赋值的布尔变量替换了其中的7个条件,因为我确信它们会被正确检查。我只保留了我原始程序中出现的最后一个条件。在最后一种情况下,将检查字符串是否为空或零,然后检查文件是否存在(因此字符串表示路径) 问题是:如果我的开关中至少有8个条件,我发现该文件从未找到。但是:只要我从开关中删除一个条件,

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

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

我把文件放在哪里也无关紧要——我试着把文件直接放在
C:\
下面。在那里也没有找到。但是,如果我单独调用
File.Exists
,而不是在return/switch中,则对该文件的搜索工作正常

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

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

返回/开关中有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
                    };
            }
        }
    }
}
AreallArgumentsPassedValid
方法只有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个值的元组时,它将。更好的方法是创建一个具有命名属性的对象,因为我认为您拥有的元组是不可能读取的。

这是
tuple
类,OP处理的是值元组。@juharr它们是被定义的。@GSerg是的,我只是在查找源代码,所以同样的问题也会存在。@Noah Stahl,明白。我用8项忽略了这个限制。您会得到一条有趣的错误消息:“操作可能会破坏运行时。”。我认为解决这个问题的一种方法是创建子元组,这样每个元组的项都不会超过7个。比如
((c1,c2,c3,c4),(c5,c6,c7,c8))
,然后你可以做比较,比如
((真,,,,,,),)
,比较短一点。