【发布时间】:2009-08-13 11:09:50
【问题描述】:
这是一个用 C# 编写的 WinForm。 假设我在我选择的目录中生成一个随机命名的文本文件。当第一次单击按钮时,我将文本框中包含的数据写入该文本文件。如果用户想对文本框中的不同数据做同样的事情,那么单击按钮应该将新数据写入文本文件而不会丢失旧数据。这就像保留日志,这可能吗?
我的代码是这样的:
private readonly Random setere = new Random();
private const string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
private string RandomString()
{
char[] buffer = new char[5];
for (int i = 0; i < 5; i++)
{
buffer[i] = chars[setere.Next(chars.Length)];
}
return new string(buffer);
}
private void button1_Click(object sender, EventArgs e)
{
DialogResult dia = MessageBox.Show("Wanna continue?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dia == DialogResult.Yes)
{
StreamWriter wFile = new StreamWriter("C:\\Users\\Ece\\Documents\\Testings\\" + RandomString() + ".txt");
wFile.WriteLine("Name Surname:" + text1.Text + text2.Text);
wFile.WriteLine("Other:" + text3.Text + text4.Text);
wFile.WriteLine("Money:" + textBox1.Text + " TL.");
wFile.WriteLine("*************************************");
wFile.Close();
}
else
{
return;
}
}
【问题讨论】: