在for循环中,索引超出范围异常

时间:2015-02-11 12:15:34

标签: c#

我必须做些傻事。所以我试图在c#项目的调试目录中运行两个可执行文件。以下是我的代码:

private void button1_Click(object sender, EventArgs e)
        {
            string imagePath = textBox1.Text;
            string outputPath = textBox2.Text;

            var imFiles = Directory.GetFiles(imagePath, "*.cm", SearchOption.AllDirectories)
                                 .Select(f => new FileInfo(f))
                                 .GroupBy(f => f.Directory.FullName, d => d, (d, f) => new { Directory = d, FirstFile = f.ToList().First() })
                                 .ToList();

            //files.ForEach(f => Console.WriteLine("{0} {1}", f.Directory, f.FirstFile));
            int cnt = imFiles.Count();

            for (int j = 0; j < cnt; j++)
            {
                string imPath = imFiles[j].FirstFile.ToString();
                string fname = imFiles[j].FirstFile.Name;
                //Convert to nifti
                string ccm = "-o " + outputPath + " -g N -r N " + imPath;

                ProcessStartInfo start = new ProcessStartInfo();
                start.FileName = "cm"; 
                start.Arguments = ccm;
                start.UseShellExecute = false;
                start.RedirectStandardOutput = true;
                start.CreateNoWindow = true;
                start.ErrorDialog = false;

                // Start the process.
                var process = Process.Start(start);
                StreamReader reader = process.StandardOutput;
                string result = reader.ReadToEnd();

                string tobesearched = "Saving ";
                string niiName = result.Substring(result.IndexOf(tobesearched) + tobesearched.Length);

                var line = niiName.Split(new[] { '\r', '\n' });
                string convPath = line[0];

                process.Close();
                reader.Close();

                //Get Nifti Header
                string mdcHdr = "-f " + convPath + " | more";

                ProcessStartInfo starts = new ProcessStartInfo();
                starts.FileName = "mdc"; // 
                starts.Arguments = mdcHdr;
                starts.UseShellExecute = false;
                starts.RedirectStandardOutput = true;

                // Start the process.
                var processs = Process.Start(starts);
                StreamReader readers = processs.StandardOutput;
                string results = readers.ReadToEnd();
                processs.Close();
                readers.Close();

                MessageBox.Show(results);

                List<string> list = new List<string>(results.Split(new string[] { "\r\n" },
                                                      StringSplitOptions.RemoveEmptyEntries));

                MessageBox.Show(list[10]);  
             }
         }  

在MessageBox.Show(list [10])行上发生错误,它返回一条消息: 指数超出范围。必须是非负数且小于集合的大小。 参数名称:index

尝试使用string []而不是list。它还返回一个超出数组范围的索引。 但是,当我取出for循环并且只运行一个项目时,它运行正常,没有任何错误。真的在这里摸不着头脑。请帮忙。欢呼声。

0 个答案:

没有答案