我注意到在我使用下面的代码创建的文件中没有换行符。在数据库中,我也存储文本,这些文本是存在的。
string story = "Critical error occurred after "
+ elapsed.ToString("hh:mm:ss")
+ "\n\n" + exception.Message;
File.WriteAllText(path, story);因此,经过一些short googling之后,我了解到我应该使用Environment-NewLine而不是文字\n来引用新行,所以我添加了如下所示。
string story = "Critical error occurred after "
+ elapsed.ToString("hh:mm:ss")
+ "\n\n" + exception.Message;
.Replace("\n", Environment.NewLine);
File.WriteAllText(path, story);但是,输出文件中没有中断行。我遗漏了什么?
发布于 2015-09-27 10:04:37
试试StringBuilder方法--它更易读,不需要记住Environment.NewLine、\n\r或\n
var sb = new StringBuilder();
string story = sb.Append("Critical error occurred after ")
.Append(elapsed.ToString("hh:mm:ss"))
.AppendLine()
.AppendLine()
.Append(exception.Message)
.ToString();
File.WriteAllText(path, story);简单解决方案:
string story = "Critical error occurred after "
+ elapsed.ToString("hh:mm:ss")
+ Environment.NewLine + exception.Message;
File.WriteAllLines(path, story.Split('\n'));发布于 2017-05-30 17:22:43
而不是使用
File.WriteAllText(path, content);使用
File.WriteAllLines(path, content.Split('\n'));发布于 2015-09-27 10:05:16
可以使用WriteLine()方法,如下所示
using (StreamWriter sw = StreamWriter(path))
{
string story = "Critical error occurred after " +elapsed.ToString("hh:mm:ss");
sw.WriteLine(story);
sw.WriteLine(exception.Message);
}https://stackoverflow.com/questions/32806677
复制相似问题