Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.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# 代码不执行_C#_File_For Loop - Fatal编程技术网

C# 代码不执行

C# 代码不执行,c#,file,for-loop,C#,File,For Loop,我知道在过去的几天里,我有点痛苦,也就是说,我有很多问题,但我一直在开发这个项目,我(形象地)离完成它只有几英寸的距离 话虽如此,我希望你在另一件事上给予帮助。这和我之前的问题有点关联,但是你不需要这些问题的代码。问题恰恰在于这段代码。我想从你那里得到的是帮助我识别它,从而解决它 在向您展示我一直在编写的代码之前,我想说一些额外的事情: 我的应用程序有一个文件合并功能,将两个文件合并在一起并处理重复条目 在任何给定的文件中,每行可以有以下四种格式之一(最后三种是可选的):卡名|金额,。卡名|金额

我知道在过去的几天里,我有点痛苦,也就是说,我有很多问题,但我一直在开发这个项目,我(形象地)离完成它只有几英寸的距离

话虽如此,我希望你在另一件事上给予帮助。这和我之前的问题有点关联,但是你不需要这些问题的代码。问题恰恰在于这段代码。我想从你那里得到的是帮助我识别它,从而解决它

在向您展示我一直在编写的代码之前,我想说一些额外的事情:

  • 我的应用程序有一个文件合并功能,将两个文件合并在一起并处理重复条目
  • 在任何给定的文件中,每行可以有以下四种格式之一(最后三种是可选的):
    卡名|金额
    。卡名|金额
    。卡名|金额
    |卡名|金额
  • 如果一行的格式不正确,程序将跳过它(完全忽略它)
  • 因此,基本上,示例文件可以如下所示:

    Blue-Eyes White Dragon|3
    ..Blue-Eyes Ultimate Dragon|1
    .Dragon Master Knight|1
    _Kaibaman|1
    
    现在,当谈到使用文件合并时,如果一行以一个特殊字符
    开头_,它应该相应地采取行动。对于
    ,它工作正常。对于以
    开头的行,它将索引移动到第二个点,最后完全忽略
    行(它们还有另一个与本讨论无关的用途)

    下面是我的merge函数代码(出于一些奇怪的原因,第二个循环中的代码根本不会执行):

    if(openFileDialog1.ShowDialog()==DialogResult.OK)
    {
    //将文件名保存到数组。
    string[]fileNames=openFileDialog1.fileNames;
    //循环浏览文件。
    foreach(文件名中的字符串文件名)
    {
    //将当前文件的所有行保存到数组中。
    string[]lines=File.ReadAllLines(文件名);
    //循环浏览文件的行。
    对于(int i=0;i

    我知道这是一段相当长的代码,但我相信所有这些都与某一点有关。我跳过了一些不重要的部分(在所有这些都执行之后)因为它们完全不相关。

    我花了太多时间阅读此类代码,以便在大海捞针。你真的需要开始使用干净代码等技术,使你的代码更具可读性,从而从一开始就更少出错。我知道这不是你问题的答案,但它会的帮助您长期理解和调试您自己的代码。@meilke我知道我的代码不是最容易理解的。自从我开始自学编程以来,我(错误地)采取了“如果行得通,就行”的立场,而不是强调我的代码结构。我肯定会研究一下,朋友。新文件设置在哪里?新文件是空的吗?当表单的加载事件发生时,程序会循环遍历资源文件并将其每一行分配给新文件。所以,回答你的问题,resources file=empty=>newFile=empty。但这不应该是b这是一个问题,因为如果函数没有找到匹配项,它只会将其添加到newFile。Jim,它不是这样工作的。如果newFile开始时为空,您将永远不会在第一个位置输入第二个for循环,因此向newFile添加内容的代码永远不会执行。无论如何,第二个for循环看起来与您想要完成的任务不符,对吗噢
    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        // Save file names to array.
        string[] fileNames = openFileDialog1.FileNames;
    
        // Loop through the files.
        foreach (string fileName in fileNames)
        {
            // Save all lines of the current file to an array.
            string[] lines = File.ReadAllLines(fileName);
    
            // Loop through the lines of the file.
            for (int i = 0; i < lines.Length; i++)
            {
                // Split current line.
                string[] split = lines[i].Split('|');
    
                // If the current line is badly formatted, skip to the next one.
                if (split.Length != 2)
                    continue;
    
                string title = split[0];
                string times = split[1];
    
                if (lines[i].StartsWith("_"))
                    continue;
    
                // If newFile (list used to store contents of the card resource file) contains the current line of the file that we're currently looping through...
                for (int k = 0; k < newFile.Count; k++)
                {
                    if (lines[i].StartsWith(".."))
                    {
                        string newTitle = lines[i].Substring(
                            lines[i].IndexOf("..") + 1);
    
                        if (newFile[k].Contains(newTitle))
                        {
                            // Split the line once again.
                            string[] secondSplit = newFile.ElementAt(
                                newFile.IndexOf(newFile[k])).Split('|');
                            string secondTimes = secondSplit[1];
    
                            // Replace the newFile element at the specified index.
                            newFile[newFile.IndexOf(newFile[k])] =
                                string.Format("{0}|{1}", newTitle, int.Parse(times) + int.Parse(secondTimes));
                        }
                        // If newFile does not contain the current line of the file we're looping through, just add it to newFile.
                        else
                            newFile.Add(string.Format(
                                            "{0}|{1}",
                                            newTitle, times));
    
                        continue;
                    }
    
                    if (newFile[k].Contains(title))
                    {
                        string[] secondSplit = newFile.ElementAt(
                            newFile.IndexOf(newFile[k])).Split('|');
                        string secondTimes = secondSplit[1];
    
                        newFile[newFile.IndexOf(newFile[k])] =
                            string.Format("{0}|{1}", title, int.Parse(times) + int.Parse(secondTimes));
                    }
                    else
                    {
                        newFile.Add(string.Format("{0}|{1}", title, times));
                    }
                }
            }
        }
    
        // Overwrite resources file with newFile.
        using (StreamWriter sw = new StreamWriter("CardResources.ygodc"))
        {
            foreach (string line in newFile)
                sw.WriteLine(line);
        }