简体   繁体   English

将文本框输出到文本文件不包含CR

[英]Outputting text boxes to text file doesn't include CR

I've got a problem whereby I've created an application where the user enters text into various text boxes. 我在创建一个应用程序时遇到了一个问题,用户可以在其中将文本输入到各个文本框中。 They then click a button which outputs it all into a text log file in a specific format. 然后,他们单击一个按钮,将其全部输出到特定格式的文本日志文件中。

I create a string which I output to a file and that string is compiled from various pieces of text and the contents of various form elements. 我创建一个字符串,然后将其输出到文件,该字符串是由各种文本和各种表单元素的内容编译而成的。

When it outputs, each separate line which has been created as part of the string creation outputs with CR LF (\\r\\n) which is how I want it, but any text which was entered into a Rich Text Box outputs with only LF (\\n) 输出时,作为字符串创建的一部分而创建的每一行都输出CR LF(\\ r \\ n),这是我想要的方式,但是输入到Rich Text Box中的任何文本仅输出LF( \\ n)

Code goes like: 代码如下:

string[] lines = {

     @"HEADER TEXT HERE",
     @"-----------------------------------------------",
     Text_Box.Text,
     Rich_Text_Box.Text,

........ ........

Directory.CreateDirectory(@"\\basedirectory\" + project_name_tb.Text + @"\" + strDate);

System.IO.File.WriteAllLines(@"\\basedirectory\" + project_name_tb.Text + @"\" + strDate
+ @"\" + strDate + @"_" + project_name_tb.Text + @"_"
+ session_number_mtb.Text + @".txt", lines);

The rich text boxes are multiline. 富文本框为多行。

How do I make the rich text box output CR LF? 如何使富文本框输出CR LF?

您可以尝试用CRLF替换所有LF实例:

Rich_Text_Box.Text.Replace("\n", "\r\n")

I think this will solve your problem even it's safe if it's already \\r\\n it won't replace it. 我认为这已经可以解决您的问题,即使它已经安全了也不会替代它。

    public string ReplaceRichTextBoxContent(string data)
    {
        return Regex.Replace(data, "(?<!\r)\n", "\r\n");
    }

use it like 用起来像

string[] lines = {

 @"HEADER TEXT HERE",
 @"-----------------------------------------------",
 Text_Box.Text,
 ReplaceRichTextBoxContent(Rich_Text_Box.Text),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM