如何在VS2013(c#)中从winform生成日志
我正在visual studio 2013(c#)中开发winforms。我有一个记录所有事件的列表框。我需要将此日志单独存储在记事本中。我如何才能做到这一点。 当我点击一个按钮时,消息应该分别存储在列表框和记事本中如何在VS2013(c#)中从winform生成日志,c#,winforms,visual-studio-2013,C#,Winforms,Visual Studio 2013,我正在visual studio 2013(c#)中开发winforms。我有一个记录所有事件的列表框。我需要将此日志单独存储在记事本中。我如何才能做到这一点。 当我点击一个按钮时,消息应该分别存储在列表框和记事本中 private void button1_Click(object sender, EventArgs e) { if (button1.Text.Equals("DOOR OPEN")) { if (true =
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";
}
}
}
在日志方法中,不要将项添加到列表框,而是执行以下操作:
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");
请记住,这只是一个示例,说明如何将一行文本附加到文件中,而不是一个日志记录系统。您使用的是什么日志记录系统。请显示代码。请告诉我们您尝试了什么,以及您的代码是什么样子。我尝试了您的方法。由于路径无法访问,我收到了一个错误。数据记录系统应该能够在不影响应用程序的情况下失败。@TomBlodget您是对的,但我不是在这里创建记录系统,对于想登录“记事本”的用户来说,这只是一个简单的答案;)@ap87使用“\”而不是“/”,并在路径末尾使用文件名,例如@“C:\Users\AP140563\Documents\Visual Studio2013\Projects\AMSimulator\AMSimulator\bin\Debug\log.txt”,更重要的是,相对于应用程序更好地处理文件,我将在answer@RezaAghaei谢谢…它成功了。。。使用“\”而不是“/”。这是一个错误。。所以我只使用/这个。。。