Mam taki oto kod. I teraz mam porównać czy tekst wpisywany przez użytkownika znajduje się już w pliku tekstowym, jak to zrobić?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Czytam Dane....\n");
string[] data = File.ReadAllLines("data.txt");
List<Persons> output = new List<Persons>();
char[] delimiters = { ',' };
foreach (string line in data)
{
Persons tmp = new Persons();
string[] parsedLine = line.Split(delimiters);
tmp.Name = parsedLine[0];
tmp.Surname = parsedLine[1];
int.TryParse(parsedLine[2], out tmp.Age);
output.Add(tmp);
}
foreach (Persons singlePerson in output)
{
Console.Write(singlePerson.ToString());
Console.WriteLine("---------");
}
string tn;
Console.WriteLine("Czy chcesz cos dopisac? (t/n)\n");
tn = Console.ReadLine();
if (tn == "t")
{
string name, surname, calosc;
int age;
Console.WriteLine("Wpisz imie\n");
name = Console.ReadLine();
Console.WriteLine("Wpisz nazwisko\n");
surname = Console.ReadLine();
Console.WriteLine("Wpisz wiek\n");
age = Console.Read();
calosc = "" + name + "," + surname + "," + age.ToString() + ","; // Obejście głupiego błędu
FileStream file = new FileStream("data.txt", FileMode.Append, FileAccess.Write);
StreamWriter sw = new StreamWriter(file);
sw.WriteLine(calosc);
sw.Close();
}
else
{
}
}
public class Persons
{
public string Name;
public string Surname;
public int Age;
public Persons() : this("N/A", "N/A", -1) { }
public Persons(string name, string surname, int age)
{
this.Name = name;
this.Surname = surname;
this.Age = age;
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("Osoba :");
sb.AppendLine("Imie: " + this.Name);
sb.AppendLine("Nazwisko: " + this.Surname);
sb.AppendLine("Wiek: " + this.Age.ToString());
return sb.ToString();
}
}
}
}