我正在使用visual studio 2013(c#)中的winforms。我有一个列表框,其中记录了所有事件。我需要将此日志也单独存储在记事本中。我该怎么做。 单击按钮时,消息应单独存储在列表框和记事本中。
private void button1_Click(object sender, EventArgs e)
{
if (button1.Text.Equals("DOOR OPEN"))
{
if (true == DescendingTime.Checked)
{
ListDataBox.Items.Insert(0, DateTime.Now + " Door Opened ...");
button1.Text = "DOOR CLOSE";
var path = "C:/Users/AP140563/Documents/Visual Studio2013/Projects/AMSimulator/AMSimulator/bin/Debug/log";
var message = "Door Opened ...";
System.IO.File.AppendAllLines(path, new string[]{message});
}
else
{
ListDataBox.Items.Add(DateTime.Now + " Door Opened ...");
button1.Text = "DOOR CLOSE";
}
}
else if (button1.Text.Equals("DOOR CLOSE"))
{
if (true == DescendingTime.Checked)
{
ListDataBox.Items.Insert(0, DateTime.Now + " Door Closed ...");
button1.Text = "DOOR OPEN";
}
else
{
ListDataBox.Items.Add(DateTime.Now + " Door Closed ...");
button1.Text = "DOOR OPEN";
}
}
}
答案 0 :(得分:1)
在Log方法中,不是向ListBox添加项目,而是执行以下操作:
var path = "Path to your log file";
var message = "Message to log"
System.IO.File.AppendAllLines(path, new string[]{message});
路径的一个例子可能是:
var path= System.IO.Path.Combine(Application.StartupPath, "Log.txt");
请记住,这只是一个如何将一行文本附加到文件而不是一个日志记录系统的示例。