4

出力をこのファイルに書き込んでいますが、Organized.txt では空のままです。最後の foreach ループを sr2.WriteLine から変更し、WriteLine を使用してコンソールにのみ書き込むと、出力がコンソールに正しく表示されるのに、テキスト ファイルに正しく表示されないのはなぜですか?

 class Program
    {
        public static void Main()
        {

            string[] arr1 = new string[200];


            System.IO.StreamWriter sr2 = new System.IO.StreamWriter("OrganizedVersion.txt");




                    // Dictionary, key is number from the list and the associated value is the number of times the key is found
                    Dictionary<string, int> occurrences = new Dictionary<string, int>();
                    // Loop test data
                    foreach (string value in File.ReadLines("newWorkSheet.txt"))
                    {
                        if (occurrences.ContainsKey(value)) // Check if we have found this key before
                        {
                            // Key exists. Add number of occurrences for this key by one
                            occurrences[value]++;
                        }
                        else
                        {
                            // This is a new key so add it. Number 1 indicates that this key has been found one time
                            occurrences.Add(value, 1);
                        }
                    }
                    // Dump result
                    foreach (string key in occurrences.Keys)
                    {
                        sr2.WriteLine(key.ToString() + occurrences[key].ToString());
                    }               

                   Console.ReadLine();



        }
    }
4

2 に答える 2