To save a list of strings to a text file in C#, you can use the System.IO.File.WriteAllLines method, which writes an array of strings to a file. Here's an example:
List<string> lines = new List<string> { "This is line 1", "This is line 2", "This is line 3" }; string path = @"C:\example.txt"; File.WriteAllLines(path, lines); In this example, a List<string> called lines is created with three strings. The File.WriteAllLines method is called with the path of the file to write to (@"C:\example.txt") and the lines list. The WriteAllLines method writes each string in the list to a new line in the file, and creates the file if it does not already exist.
If you want to append the list to an existing file instead of overwriting it, you can use the File.AppendAllLines method instead:
List<string> lines = new List<string> { "This is line 4", "This is line 5", "This is line 6" }; string path = @"C:\example.txt"; File.AppendAllLines(path, lines); In this example, a new List<string> called lines is created with three more strings. The File.AppendAllLines method is called with the same file path (@"C:\example.txt") and the lines list. The AppendAllLines method appends each string in the list to a new line in the existing file, without overwriting any existing content.
Note that if you are working with large amounts of data, or if you need more control over the formatting of the output, you may want to use a different approach, such as writing to a StreamWriter or using a third-party library such as CsvHelper.
"C# save List to txt file example"
List<string> myList = new List<string> { "item1", "item2", "item3" }; File.WriteAllLines("output.txt", myList); "C# write List elements to txt file"
List<int> myNumbers = new List<int> { 1, 2, 3, 4, 5 }; File.WriteAllLines("output.txt", myNumbers.Select(n => n.ToString())); "C# save List of objects to txt file"
List<Person> peopleList = GetPeopleList(); File.WriteAllLines("output.txt", peopleList.Select(p => $"{p.Name},{p.Age},{p.City}")); "C# append List to existing txt file"
List<string> additionalData = new List<string> { "newItem1", "newItem2" }; File.AppendAllLines("output.txt", additionalData); "C# save List as CSV to txt file"
List<string> csvData = new List<string> { "name,age,city", "John,25,New York", "Alice,30,Los Angeles" }; File.WriteAllLines("output.csv", csvData); "C# write List of objects to JSON txt file"
List<Person> peopleList = GetPeopleList(); string jsonData = JsonConvert.SerializeObject(peopleList); File.WriteAllText("output.json", jsonData); "C# save List to txt file with specific format"
List<string> myList = new List<string> { "item1", "item2", "item3" }; string formattedData = string.Join(Environment.NewLine, myList.Select(item => $"- {item}")); File.WriteAllText("output.txt", formattedData); "C# save List of integers to txt file"
List<int> numbersList = new List<int> { 10, 20, 30, 40, 50 }; File.WriteAllLines("output.txt", numbersList.Select(n => n.ToString())); "C# save List to txt file with line numbers"
List<string> myList = new List<string> { "item1", "item2", "item3" }; string numberedData = string.Join(Environment.NewLine, myList.Select((item, index) => $"{index + 1}. {item}")); File.WriteAllText("output.txt", numberedData); "C# save List to txt file with custom delimiter"
List<string> myList = new List<string> { "John", "Doe", "30" }; string delimitedData = string.Join(",", myList); File.WriteAllText("output.txt", delimitedData); stream angularfire2 graphicsmagick linear-equation iequalitycomparer transducer twitter-bootstrap-2 timeoutexception pylint dashboard