How can StreamWriter write characters to a specific string? For example, the file looks like this. It is necessary to write "Vasily" in the first line, "31" in the second .. enter image description here

  • if you want to record formatted (in a certain sequence), then you do the serialization of the object (s), and then write to the file what happened after serialization. - Bulson
  • @FoggyFinder I am not familiar with this yet - ZOOM SMASH
  • one
    If you do not try to learn something new, then never go beyond your level. Dare! - user227049
  • Use TextWriter if you are working with text, and its method WriteLine is rdorn

1 answer 1

Here without serialization, using ToString()

 class Person { public int Age { get; set; } public string Name { get; set; } public string WorkPlace { get; set; } public override string ToString() { return $"Имя: {this.Name},{Environment.NewLine}Возраст: {this.Age},{Environment.NewLine}Рабочее место: {this.WorkPlace}"; } } class Program { static void Main(string[] args) { Person person = new Person() { Age = 31, Name = "Вася", WorkPlace = "Цех самоубийц" }; string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); path = Path.Combine(path, "people.txt"); using (FileStream fs = File.OpenWrite(path)) using (StreamWriter sw = new StreamWriter(fs)) { sw.Write(person.ToString()); } } }