【问题标题】:How can I format the output of the File.WriteTextAll function?如何格式化 File.WriteTextAll 函数的输出?
【发布时间】:2015-01-05 16:36:46
【问题描述】:

对于这个项目,我将输入一个 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

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

【问题讨论】:

  • 您可以使用 SELECT 硬编码列名 UNION ALL。
  • 使用 StringBuilder 而不是在循环中附加到 String - 它要快得多。

标签: c# sql winforms


【解决方案1】:

您可以像这样在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;
}

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 我对StringBuilder不熟悉,我会研究一下。谢谢。
    猜你喜欢
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 2021-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-14
    • 1970-01-01
    相关资源
    最近更新 更多