我希望程序运行,每次用户点击按钮,然后我想将其添加到文件中,截至目前,程序会使用用户输入的新信息覆盖内容。
private void tradeMethod()
{
newTrade.Analysis = textBox1.Text;
newTrade.Trend = textBox2.Text;
newTrade.entryLevel = double.Parse(textBox3.Text);
newTrade.bottomLine = double.Parse(textBox4.Text);
newTrade.Picture = textBox5.Text;
newTrade.entrySetup = textBox6.Text;
string Analysis = newTrade.Analysis;
string Trend = newTrade.Trend;
double entryLevel = newTrade.entryLevel;
double bottomLine = newTrade.bottomLine;
string Picture = newTrade.Picture;
string entrySetup = newTrade.entrySetup;
string[] info = { "The analysis: "+Analysis,"The Trend: " +Trend, "The entry price: " + entryLevel.ToString(),
"The profit/loss of the trade: " + bottomLine.ToString(),"The entry of the trade was: " + entrySetup, "The screnshot of the picture: " +Picture};
System.IO.File.WriteAllLines(@"C:\\Users\\Samuel\\Documents\\Visual Studio 2013\\Projects\\WindowsFormsApplication8\\WindowsFormsApplication8\\trade.txt", info);
}
private void button1_Click(object sender, EventArgs e)
{
tradeMethod();
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void button3_Click(object sender, EventArgs e)
{
try
{
StreamReader inputFile;
string addrLine;
inputFile = File.OpenText("C:\\Users\\Samuel\\Documents\\Visual Studio 2013\\Projects\\WindowsFormsApplication8\\WindowsFormsApplication8\\trade.txt");
while (!inputFile.EndOfStream)
{
addrLine = inputFile.ReadLine();
MessageBox.Show(addrLine);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
答案 0 :(得分:0)
Alessandro D'Andria的评论是正确的。使用方法:
System.IO.File.WriteAllLines
每次都会覆盖该文件,删除所有以前的内容。根据{{3}}文档,您应该使用:
System.IO.File.AppendAllLines
如果不存在新文件,此方法将处理创建新文件。