1

I must be doing something silly. So Iā€™m trying to run two executables in the debug directory of the c# project. Below is my code:

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

On the line MessageBox.Show(list[10]) an error occured and it returns a message as such: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

Tried with string[] instead of list. And it also returns a index out of bounds of array. However when I take the for loop out and only run one item it runs fine without any error. Really scratching my head here. Please help. Cheers.

  • 1
    You need to debug your code. Look at the contents of the list and see what is in there in each situation - either through inspection or by writing the contents to the console/trace and see whats in there. That may give you some clues as to what is going wrong. Its impossible to really say any more without knowing what it is you're trying to do. What we can almost certainly say is that for whatever reason "list" does not have a 11th element (index 10) ā€“ Tim Rutter Feb 11 '15 at 12:22
  • I noticed that you use the same filename to run both processes. How do you know if the first process has ended before the second one starts? Perhaps that's why the code runs fine on its own but fails when placed in a loop. Also, try MessageBox.Show(list.Count); and see what the size is before you call the non-existing index. ā€“ display name Feb 11 '15 at 12:28
  • the list doesn't contain 11 item .. that's why you have this error .. try to debug your code and check how many item inside the list ā€“ Elie M Feb 11 '15 at 12:32

0 Answers0