Как записать текстовый файл из нескольких методов
Здравствуйте, я пытаюсь записать текстовый файл из нескольких методов, но не могу получить информацию в моем втором методе (Getuserdetails), чтобы перейти в файл или в то место, где мне это нужно (между началом и концом).
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace System.IO { class Program { static void Main(string[] args) { mainMenu(); Console.WriteLine("Happy coding"); } public static void mainMenu() { int value = 0; StreamWriter writer = new StreamWriter("numbers.txt"); do { Console.WriteLine("Menu \n 1. Main Method \n 2. Method \n 0. End"); value = Convert.ToInt32(Console.ReadLine()); switch (value) { case 1: writer.WriteLine("MAIN \nBEGIN"); GetUserDetails(); writer.WriteLine(); writer.WriteLine("END"); break; case 2: writer.WriteLine(); writer.WriteLine("METHOD \nBEGIN"); writer.WriteLine(); writer.WriteLine("END"); GetUserDetails(); break; } } while (value > 0); writer.Close(); } public static void GetUserDetails() { int value = 0; int a = 0; StreamWriter writer = new StreamWriter("numbers.txt"); do { Console.WriteLine("Menu \n 1. CREATE \n 2. PRINT \n 3. ← READ user input \n 4. IF ELSE \n 5. SWITCH CASE \n 0. Exit"); value = Convert.ToInt32(Console.ReadLine()); switch (value) { case 1: Console.WriteLine("How many CREATES would you like?"); a = Convert.ToInt32(Console.ReadLine()); do { writer.WriteLine("CREATE"); a = a - 1; } while (a > 0); writer.WriteLine(); break; case 2: Console.WriteLine("How many PRINT would you like?"); a = Convert.ToInt32(Console.ReadLine()); do { writer.WriteLine("PRINT"); a = a - 1; } while (a > 0); writer.WriteLine(); break; case 3: Console.WriteLine("How many ← READ user input would you like?"); a = Convert.ToInt32(Console.ReadLine()); do { writer.WriteLine("← READ user input"); a = a - 1; } while (a > 0); writer.WriteLine(); break; case 4: Console.WriteLine("How many ELSE would you like in your IF statement?"); a = Convert.ToInt32(Console.ReadLine()); writer.WriteLine("IF"); int b = 0; do { b = b + 1; writer.WriteLine("ELSE " + b); a = a - 1; } while (a > 0); writer.WriteLine(); break; case 5: Console.WriteLine("How many CASE would you like in your SWITCH statement?"); a = Convert.ToInt32(Console.ReadLine()); writer.WriteLine("SWITCH"); b = 0; do { b = b + 1; writer.WriteLine("CASE " + b); a = a - 1; } while (a > 0); writer.WriteLine(); break; } } while (value > 0); } } } What I have tried: I will greatly appreciate any help.
BillWoodruff
Если вашей программе нужно вставить текст в файл в определенной позиции или в начале (prepend), вам нужно будет перестроить файл для достижения этой цели.