static void Main(string[] args) { string[] files = Directory.GetFiles (@"C:\Users\mostafaizz\Documents\Visual Studio 2013\Projects\ConsoleApplication1\Release\errors_helicopter_recorded_spatial_redundancy", "*.frames", SearchOption.AllDirectories); Dictionary <string, List <string> > dic = new Dictionary <string, List <string> >(); // get the same files together for (int i = 0; i < files.Length; i++) { FileInfo finf = new FileInfo(files[i]); string key = finf.Name.Substring(0, 10); if (!dic.ContainsKey(key)) { dic.Add(key, new List <string>()); } dic[key].Add(files[i]); } // foreach (KeyValuePair <string, List <string> > entry in dic) { StreamReader GT, test; if (entry.Value[0].Length > entry.Value[1].Length) { GT = new StreamReader(entry.Value[0]); test = new StreamReader(entry.Value[1]); } else { GT = new StreamReader(entry.Value[1]); test = new StreamReader(entry.Value[0]); } // then read the values and write the correlated signals List <double> GTList = new List <double>(), testList = new List <double>(); while (!GT.EndOfStream) { string tmp = GT.ReadLine(); double data; if (double.TryParse(tmp, out data)) { GTList.Add(data); } } GT.Close(); while (!test.EndOfStream) { string tmp = test.ReadLine(); double data; if (double.TryParse(tmp, out data)) { testList.Add(data); } } test.Close(); // write to file writeToFileHighCrossCorrelation(GTList.ToArray(), testList.ToArray(), entry.Key); } }