private const string FILE_NAME = "E:/aaaa/m5.txt";
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (File.Exists(FILE_NAME))
{
Console.WriteLine("{0} already exists.", FILE_NAME);
return;
}
using (StreamWriter sw = File.CreateText(FILE_NAME))
{
sw.WriteLine("This is my file.");
sw.WriteLine("I can write ints {0} or floats {1}, and so on.",
1, 4.2);
sw.Close();
}
}
private void button2_Click(object sender, EventArgs e)
{
if (!File.Exists(FILE_NAME))
{
Console.WriteLine("{0} does not exist.", FILE_NAME);
return;
}
using (StreamReader sr = File.OpenText(FILE_NAME))
{
String input;
while ((input = sr.ReadLine()) != null)
{
this.textBox1.Text = input;
}
Console.WriteLine("The end of the stream has been reached.");
sr.Close();
}
}