Загрузка...

Как можно вытащить определенные данные из файла?

Тема в разделе C# создана пользователем Ebaysher 28 авг 2021. 128 просмотров

  1. Ebaysher
    Ebaysher Автор темы 28 авг 2021 0 4 окт 2020
    Происходит запись в текстовый файл таких свойств как Автор, Имя, Фамилия, Организация, ИИН, ID и т.д
    как можно вывести из файла только Имя и Фамилию?
    Код
    static void Main(string[] args)
    {
    string path = "C:\\textFile.txt";

    Console.WriteLine("1 - Зарегистрироваться как физическое лицо");
    Console.WriteLine("2 - Зарегистрироваться как юридическое лицо");
    Console.WriteLine("3 - Просмотреть список всех зарегистрированных лиц");
    Console.WriteLine("4 - Сделать вывод списка физ лиц. Упорядочить список физ. лиц по Фамилии, Имени, Отчеству. ");

    byte pick = Convert.ToByte(Console.ReadLine());
    List<Person> people = new List<Person>();
    List<Organization> peopleOrg = new List<Organization>();


    if (pick == 1)
    {
    Console.WriteLine("Автор - ");
    string _creator = Console.ReadLine();
    Console.WriteLine("Имя - ");
    string _name = Console.ReadLine();
    Console.WriteLine("Фамилия - ");
    string _lastName = Console.ReadLine();
    Console.WriteLine("ИИН - ");
    string _iin = Console.ReadLine();

    people.Add(new Person(_creator, _name, _lastName, _iin));

    using (StreamWriter sw = new StreamWriter(path, true, System.Text.Encoding.Default))
    {
    foreach (var item in people)
    {
    sw.WriteLine();
    sw.Write($"{item.Name}\t");
    sw.Write($"{item.LastName}\t");
    sw.Write($"{item.Creator}\t");
    sw.Write($"{item.Id}\t");
    sw.Write($"{item.Iin}\t");
    sw.Write($"{item.DateOfCreaction}\t");
    }
    }
    }

    if (pick == 2)
    {
    Console.WriteLine("ИИН - ");
    string _iin = Console.ReadLine();
    Console.WriteLine("Автор - ");
    string _creator = Console.ReadLine();
    Console.WriteLine("Имя организации - ");
    string _orgname = Console.ReadLine();

    peopleOrg.Add(new Organization(_orgname, _creator, _iin));

    using (StreamWriter sw = new StreamWriter(path, true, System.Text.Encoding.Default))
    {
    foreach (var item in peopleOrg)
    {
    sw.WriteLine();
    sw.Write($"{item.Creator}\t");
    sw.Write($"{item.OrganizationName}\t");
    sw.Write($"{item.Id}\t");
    sw.Write($"{item.Iin}\t");
    sw.Write($"{item.DateOfCreaction}\t");
    }
    }
    }

    if (pick == 3)
    {
    using (StreamReader sr = new StreamReader(path))
    {
    Console.WriteLine(sr.ReadToEnd());
    }
    }

    if (pick == 4)
    {

    }

    }
    Код
    class CounterAgent
    {
    public Guid Id { get; }
    public string Iin { get; set; }
    public DateTime DateOfCreaction { get; set; } //Дата создания
    public string Creator { get; set; } //Автор
    public DateTime DateOfChange { get; set; } // Дата изменения
    public string ChangeAuthor { get; private set; } //Автор изменения

    public CounterAgent(string creator, string iin)
    {
    this.Creator = creator;
    this.DateOfCreaction = DateTime.Now;
    this.Id = Guid.NewGuid();
    if (iin.Length == 12)
    {
    this.Iin = iin;
    }
    else throw new ArgumentException("ИИН должен состоять из 12 символов!");
    }
    }
    Код
    class Person : CounterAgent
    {
    public string Name { get; set; }
    public string LastName { get; set; }
    public Person(string name ,string lastName,string creator, string iin) : base(creator, iin)
    {
    Name = name;
    LastName = lastName;
    }
    }
    Код
    class Organization : CounterAgent
    {
    public string OrganizationName { get; set; }
    public Organization(string orgName, string creator, string iin) : base(creator, iin)
    {
    this.OrganizationName = orgName;
    }
    }
     
  2. Jennierubyjane
    Jennierubyjane 28 авг 2021 BLACKPINK IN YOUR AREA 5476 11 янв 2018
    Считал файл в класс и вывел все в консоль, а вообще советую написать нормально со switch case и зачем тебе нужен streamwriter, когда есть методы в system.io
     
    1. Ebaysher Автор темы
      Jennierubyjane, с if гемороя меньше. А как именно определенные свойства вывести из файла?
    2. Jennierubyjane
      Ebaysher, С if получается говнокод, зачем это делать когда есть switch? А вывод свойства обычно так же как и вывод переменной класса через объект (example.var)
Загрузка...
Top