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.