Program runs three times, gets three different results <p>I wrote this program to cut up EDI files into their own files but it is not doing it consistantly. Sometimes it breaks a file with three sections into only two sections, and other times it finds all three sections.</p> <p>Can anyone explain to me why it is behaving this way? Any help is greatly appreciated. </p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace chomp { class Program { static string GenerateFileName(string context) { return context + "_" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".edi"; } static int Main(string[] args) { string delimiter = "\r\n\r\n"; if (args.Length == 0) { Console.WriteLine("Please enter a file name: "); return 1; } else { try { string fileName = (args[0]); if (File.Exists(fileName)){ System.IO.StreamReader myFile = new System.IO.StreamReader(fileName); string FileStream = myFile.ReadToEnd(); myFile.Close(); string myFileName = GenerateFileName("DOC"); string[] FSPart = FileStream.Split(new string[] { delimiter }, StringSplitOptions.None); foreach (string s in FSPart) { string newAwesomeFN = DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".edi"; using (System.IO.FileStream fs = System.IO.File.Create(newAwesomeFN)) { } System.IO.StreamWriter file = new System.IO.StreamWriter(newAwesomeFN); file.WriteLine(s); file.Close(); } return 0; } else { System.Console.WriteLine("Filename "+ fileName+ " does not exist!"); return 0; } // closes else + if File.Exists } catch (System.FormatException) { Console.WriteLine("A Filename must be entered!"); return 1; } } } //closes main } //closes program } //closes main </code></pre>