Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将多行字符串保存到多行文本文件中_C# - Fatal编程技术网

C# 将多行字符串保存到多行文本文件中

C# 将多行字符串保存到多行文本文件中,c#,C#,我下面有一段代码,可以很好地保存到文本文件中,但是没有传输格式,例如,下面的单词将另存为ThisisTestLayout。我希望所需的输出显示在下面: THIS IS A TEST LAYOUT 我知道我这里有点明显的遗漏,所以谢谢你的帮助 if (entity.Switch == true) { string path = @"filepath....\\Test.txt"; if (!File.Exists(

我下面有一段代码,可以很好地保存到文本文件中,但是没有传输格式,例如,下面的单词将另存为
ThisisTestLayout
。我希望所需的输出显示在下面:

THIS
IS 
A
TEST
LAYOUT
我知道我这里有点明显的遗漏,所以谢谢你的帮助

        if (entity.Switch == true)
        {
            string path = @"filepath....\\Test.txt";
            if (!File.Exists(path))
            {
                // Create a file to write to. 
                using (StreamWriter sw = File.CreateText(path))
                {
                    sw.Write(entity.comments);
                }
            }
         }
该字符串保存在一个名为comments的实体中,并在C#应用程序中显示良好

sw.Write
sw.WriteLine
都提供相同的单行输出,没有空格


此外,entity.comments引用SQL Server表中的一列,并且是VARCHAR(MAX)

更改
sw.Write
sw.WriteLine

您可能在换行时使用“\n”。如果是,请使用

sw.Write(entity.comments.Replace("\n", "\r\n"));

这与上面描述的效果完全相同,您确定吗?您已经比较了
Write
WriteLine
的输出,它们完全相同?是的,文本本身存储在SQL Server数据库中,注释本身是一个VARCHAR(MAX)因此,将屏幕上显示的数据传输到文本文件时,似乎存在格式问题
实体的值是多少。注释
?如何确定输出中没有换行符?entity.comments与字符串comments相同,它的被调用实体使用Lightswitch到期。评论的价值就是我上面举的例子,你举的两个例子中的哪一个?我的意思是,
entity.comments
是否已经包含换行符,如果已经包含,那么是哪种格式?entity.comments引用SQL Server表,而comments是varchar(MAX),就像换行符是以unix样式编码的。将“\n”和“r”替换为
Environment.NewLine
。我必须等待,直到它允许我,50秒;)<代码>环境。换行符
将优于
“\r\n”