come scrivere un file di testo c# code example
Example 1: aprire e scrivere un file in c#
try
{
StreamWriter sw = new StreamWriter("C:\\Test.txt");
sw.WriteLine("Hello World!!");
sw.WriteLine("From the StreamWriter class");
sw.Close();
}
catch(Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
finally
{
Console.WriteLine("Executing finally block.");
}
Example 2: aprire e scrivere un file in c#
String line;
try
{
StreamReader sr = new StreamReader("C:\\Sample.txt");
line = sr.ReadLine();
while (line != null)
{
Console.WriteLine(line);
line = sr.ReadLine();
}
sr.Close();
Console.ReadLine();
}
catch(Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
finally
{
Console.WriteLine("Executing finally block.");
}