在我的应用中,我正在确定网络发生变化。我有一些我正在使用的示例代码,但我想以某种方式格式化结果字符串
string change = string.Empty;
switch (e.NotificationType)
{
case NetworkNotificationType.InterfaceConnected:
change = "Connected to ";
break;
case NetworkNotificationType.InterfaceDisconnected:
change = "Disconnected from ";
break;
case NetworkNotificationType.CharacteristicUpdate:
change = "Characteristics changed for ";
break;
default:
change = "Unknown change with ";
break;
}
string changeInformation = String.Format(" {0} {1} {2} ({3})",
DateTime.Now.ToString(), change, e.NetworkInterface.InterfaceName,
e.NetworkInterface.InterfaceType.ToString());
// We are making UI updates, so make sure these happen on the UI thread.
Dispatcher.BeginInvoke(() =>
{
Changes.Add(changeInformation); //Changes contains the changeInformation
});
目前结果似乎是
但这根本没有很好的格式化,并且往往会混乱。我希望能够对其进行格式化,以便DateTime.Now.ToString()
位于第一行,然后change
位于下一行,然后是e.NetworkInterface.InterfaceName
和e.NetworkInterface.InterfaceType.ToString()
。我怎么能这样做?
答案 0 :(得分:1)
使用明确的\r\n
或更好的Environment.NewLine
,例如
String.Format(" {0}{1}{2}{3}{4}({5})",
DateTime.Now, Environment.NewLine,
change, Environment.NewLine,
e.NetworkInterface.InterfaceName,
e.NetworkInterface.InterfaceType);
另请注意,您在参数中不需要裸体.ToString()
- 这隐含在String.Format()
答案 1 :(得分:0)
我认为你必须添加新行
string changeInformation = String.Format(" {0} {1} {2} {3} ({4})",
DateTime.Now.ToString(), Environment.NewLine, change, e.NetworkInterface.InterfaceName,
e.NetworkInterface.InterfaceType.ToString());