如何格式化 File.WriteTextAll 函数的输出

本文关键字:函数 输出 WriteTextAll File 格式化 | 更新日期: 2023-09-27 18:36:12

对于这个项目,我正在获取SQL数据库的输入并将其放入txt文件中,以便于传输,而不是其他什么。

到目前为止,它可以工作,但我想让它更容易阅读。当此应用运行时,它会每 60 秒更新一次文件。我想在列中添加标题,以便阅读它的人可以知道每列代表什么。我不知道如何添加标题,以便每次刷新插入标题下方添加新文本。

这就是我到目前为止所拥有的

private void Output()
        {
            string createText = "";

            foreach (KeyValuePair<string, AlarmData> AlarmDataText in AlarmDictionary)
            {
                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;
                createText += Environment.NewLine;
            }
            if (true)
            {
            }
            File.WriteAllText("C:''Users''%username%''Documents''Visual Studio 2010''Projects''NMS_Logger''NMS_Logger''bin''Log.txt", createText);
        }//end Output()

这是它在文本文件中如何出现的示例

Major   2015-01-05 11:15  2014-11-11 09:55  10.10.10.1               apSysMgmtInet
Major   2015-01-05 11:15  2014-11-11 09:56  10.10.10.1               apSysMgmtInet
Major   2015-01-05 11:16  2014-11-13 12:35  10.10.10.1    bwCNAMS    C N A M Server 
Info    2015-01-05 11:17  2015-01-02 03:29  10.10.10.1               CIT Debug Trap 
Major   2015-01-05 11:15  2014-11-14 09:48  10.10.10.1    RAI        telicaT1Alarm 
Major   2015-01-05 11:17  2014-11-20 02:11  10.10.10.1               portErrorsExceeded 
Info    2015-01-05 11:15  2015-01-05 11:14  10.10.10.1    NORMAL     telicaT1Event 
Major   2015-01-05 11:15  2014-11-14 09:48  10.10.10.1    RAI        telicaT1Alarm 
Info    2015-01-05 11:15  2015-01-05 11:14  10.10.10.1    NORMAL     telicaT1Event 
Major   2015-01-05 11:17  2014-11-12 05:05  10.10.10.1               Error ENVMON
Info    2015-01-05 11:16  2014-12-03 15:43  10.10.10.1               Debug SEC

如您所见,列标签会很好。

如何格式化 File.WriteTextAll 函数的输出

你可以像

这样在createText上附加你的列名:

string createText = //just make sure that the column name doesn't exceed your padding width
        FormatWidth("Severity" 8) + 
        FormatWidth("LastNotification") +
        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;
}

在 foreach 循环之外和语句 string createText = ""; 之后,您可以将合适的列名附加到 createText 中。与其使用String不如尝试使用StringBuilder