//Kui kasutad StreamWriter või StreamReader
Siis pead kindlasti faili sulgema, näiteks:
StreamWriter sw = new StreamWriter(path);
sw.WriteLine("Midagi");
sw.Close(); // Vajalik!
//Või parem lahendus:
using (StreamWriter sw = new StreamWriter(path))
{
sw.WriteLine("Midagi");
} // Fail suletakse automaatselt siin
Faili lugemine (StreamReader)
try
{
string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Kuud.txt");
StreamReader text = new StreamReader(path);
string laused = text.ReadToEnd();
text.Close();
Console.WriteLine(laused);
}
catch (Exception)
{
Console.WriteLine("Mingi viga failiga, ei saa faili lugeda");
}