'VS C# write a Console Output into a file

I'm a beginner and looking for a solution. Need Help to Write this Code result from Console into the file. Please help. I tried all, but I did not find a good way to solve it.

     namespace ESA2Out {
         class Program {
             static string ESA2In(string verzeichnis, string dateiname, int zeilenlaenge = 0) {
                 string inhalt = string.Empty;
                 string dn = Path.Combine(verzeichnis, dateiname);
                 if (File.Exists(dn)) {
                     // Arrays einlesen
                     using(StreamReader sr = new StreamReader(dn)) {
                         inhalt = sr.ReadToEnd();
                         if (zeilenlaenge > 0) {
                             int anzahl = inhalt.Length / zeilenlaenge;
                             anzahl += inhalt.Length % zeilenlaenge > 0 ? 1 : 0;
                             string[] zeilen = new string[anzahl];
                             // Textzeilen extrahieren
                             for (int i = 0; i < anzahl; i++) {
                                 zeilen[i] = inhalt.Substring(i * zeilenlaenge, zeilenlaenge);
                             }
                             inhalt = string.Join("\r\n", zeilen);
                         }
                     }
                 } else {}
                 return inhalt;
             }

             static void ESA2Out(byte[] data, string verzeichnis, string dateiname) {
                 string dn = Path.Combine(verzeichnis, dateiname);
                 if (!File.Exists(dn)) {
                     using(FileStream fs = File.Create(dn)) {
                         fs.Write(data, 0, data.Length);
                     }
                 } else {}
             }

             static void Main(string[] args) {
                 byte[] array = { 32, 32, 67, 67, 32, 32, 32, 35, 32, 35, 32, 32, 67, 32, 32, 67, 32, 32, 35, 32, 35, 32, 67, 32, 32, 32, 32, 32, 35, 35, 35, 35, 35, 67, 32, 32, 32, 32, 32, 32, 35, 32, 35, 32, 67, 32, 32, 32, 32, 32, 35, 35, 35, 35, 35, 32, 67, 32, 32, 67, 32, 32, 35, 32, 35, 32, 32, 32, 67, 67, 32, 32, 32, 35, 32, 35, 32
                 };
                 ESA2Out(array, @ ".\\", "ESA2.txt");
                 Console.WriteLine();
                 Console.ForegroundColor = ConsoleColor.Green;
                 Console.WriteLine(ESA2In(@ ".\\", "ESA2.txt", 11));
                 Console.ResetColor();
                 Console.ReadKey();
             }
         }
     }

That is the Code, it runs exactly, but now I don't find a way to write the result into the file. If I will open that file in Editor it should show the same as in the Console before.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source