C# 如何附加到使用循环的文本文件?

C# 如何附加到使用循环的文本文件?,c#,winforms,C#,Winforms,我的应用程序从SQL数据库中获取所选信息,并将其放入一个列格式良好的文本文件中。我遇到的问题是,如果程序因任何原因崩溃或停止,它将在重新启动时覆盖以前的文本 我正试着使用,但我似乎无法正确设置 这是我用来写文本文件的方法 private void Output() { string createText = FormatWidth("Severity", 10) +

我的应用程序从SQL数据库中获取所选信息,并将其放入一个列格式良好的文本文件中。我遇到的问题是,如果程序因任何原因崩溃或停止,它将在重新启动时覆盖以前的文本

我正试着使用,但我似乎无法正确设置

这是我用来写文本文件的方法

private void Output()
    {           
                  string createText = 
                    FormatWidth("Severity", 10) + 
                    FormatWidth("LastNotification", 18) +
                    FormatWidth("FirstNotification", 18) + 
                    FormatWidth("DeviceIP", 18) + 
                    FormatWidth("DeviceInterface", 20) + 
                    "DescriptionShort";

        foreach (KeyValuePair<string, AlarmData> AlarmDataText in AlarmDictionary)
        {
            createText += Environment.NewLine;
            createText += 
            FormatWidth(AlarmDataText.Value.eventSeverity, 8) + 
            FormatWidth(AlarmDataText.Value.eventLastNotification.ToString("yyyy-MM-dd HH:mm"), 18) +
            FormatWidth(AlarmDataText.Value.eventFirstNotification.ToString("yyyy-MM-dd HH:mm"), 18) + 
            FormatWidth(AlarmDataText.Value.deviceIP, 18) + 
            FormatWidth(AlarmDataText.Value.deviceInterface, 20) + 
            AlarmDataText.Value.descriptionShort;
        }

        File.WriteAllText("C:\\Users\\*username*\\Documents\\Visual Studio 2010\\Projects\\NMS_Logger\\NMS_Logger\\bin\\Log.txt", createText);

        Console.WriteLine("File Updated"); //For working verification


    }//end Output()
private void输出()
{           
字符串createText=
格式宽度(“严重性”,10)+
FormatWidth(“LastNotification”,18)+
FormatWidth(“第一次通知”,18)+
格式宽度(“DeviceIP”,18)+
FormatWidth(“DeviceInterface”,20)+
“描述短”;
foreach(AlarmDictionary中的KeyValuePair AlarmDataText)
{
createText+=Environment.NewLine;
createText+=
FormatWidth(AlarmDataText.Value.eventSeverity,8)+
FormatWidth(AlarmDataText.Value.eventLastNotification.ToString(“yyyy-MM-dd-HH:MM”),18)+
FormatWidth(AlarmDataText.Value.eventFirstNotification.ToString(“yyyy-MM-dd-HH:MM”),18+
FormatWidth(AlarmDataText.Value.deviceIP,18)+
FormatWidth(AlarmDataText.Value.deviceInterface,20)+
AlarmDataText.Value.descriptionShort;
}
File.writealText(“C:\\Users\\*username*\\Documents\\Visual Studio 2010\\Projects\\NMS\u Logger\\NMS\u Logger\\bin\\Log.txt”,createText);
Console.WriteLine(“文件更新”);//用于工作验证
}//结束输出()

在使用c#file exist函数放置之前检查文件是否存在,或者每次运行应用程序时创建一个具有不同名称的新文件

如果要附加,您将无法使用
file.writealText
,因此我将执行以下操作:

StreamWriter sw;
if (!File.Exists(outputPath)
{
    sw = new StreamWriter(outputPath, false);
    sw.WriteLine(headers);
}
else
    sw = new StreamWriter(outputPath, true);

foreach (...)
   sw.WriteLine(dataLine);
您可以将
StreamWriter
初始化为始终附加在声明上,但它会破坏“previousfile exists”逻辑,因此我将其设置为与通常不同的一点

作为一个旁注,你正在做很多字符串连接,而不是考虑使用<代码> StringBuilder < /代码>。如果坚持这种方法,可以使用

File.AppendAllText
,而不是在
foreach
中单独写入每一行。请注意,与只将每一行写入文件相比,这种方法的内存效率非常低(感谢@Servy)


此外,如果程序崩溃,中间会有一个“垃圾”行,这将是不可检测的(为了把换行符放进去)。您可能希望在开始追加之前始终插入新行,即使这在正常情况下会导致数据中出现奇怪的换行。

所以您希望它一直在中断的位置继续,还是在崩溃的情况下继续?整个用例似乎都停止了,它正在做它应该做的事情。是的,我希望它一直在停止的地方继续。这将在特定的时间范围内运行,比如一周。我希望此文件的大小达到1TB左右。如果文件已创建,是否可以使用file.AppendAllText?链接到我所更改的内容too@ChrisPBacon是的,这种方法也会起作用(尽管你仍然需要考虑到每次都不写标题)。将每一行写到一个文件将比使用SB执行得更好。它会减少开销,并将内存占用从O(n)更改为O(1)。@Servy感谢您的评论,我将它们合并到了有关SB潜在用途的部分中/AppendAllText@BradleyDotNET你对某人的评论让我有点困惑,您是说使用SB在内存方面更有效还是不更有效,并坚持使用串联?还有,在我的代码中(标题)与什么相关?