[英]How can I format the output of the File.WriteTextAll function?
對於這個項目,我將輸入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
如您所見,列標簽會很好。
您可以像這樣將列名稱附加在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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.