How to save a multi-line text field as a single line in a text file?

Ok guys, I am creating a function to save a file. I encountered a problem in that when I save data from multi-line text fields, it saves x the number of lines as the number of lines in the text file.

So, for example, if the user entered:

line one
line two
line three

It will display as:

line one
line two
line three

as I want it to display as:

line one \n line two \n line three \n

The code I have is:

                savefile.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                savefile.Title = "Save your file";
                savefile.FileName = "";
                savefile.Filter = "ChemFile (*.cd)|*.cd|All Files|*.*";

                if (savefile.ShowDialog() != DialogResult.Cancel)
                {                    
                   // save the text file information
                    for (int i = 0; i < noofcrit; i++)
                    {
                        cdfile[i] = crittextbs[i].Text;
                    }                    
                }
                    // Compile the file
                    SaveFile = savefile.FileName;
                    System.IO.File.WriteAllLines(SaveFile, cdfile);

Any ideas how I can save multi-line text files as one line? Thank.

+5
source share
4 answers

Newline @" \n " " \\n ", @, escape char

   string s= yourTextBox.Text.Replace(Environment.NewLine, @" \n "));
+4

, - . , escape-. , StreamWriter.

string myData = txtMyTextBox.Text.Replace("\r"," \\r ").Replace("\n"," \\n ");
using(System.IO.StreamWriter sw = new System.IO.StreamWriter(filePath))
{
     sw.Write(myData);
}
+2

, :

string[] multiline = new []{"multi","line","text"};
string singleLine = string.Join(@"\n",multiline);

, Replace ,

string singleLine = multiline.Replace("\r",string.Empty).Replace("\n",@"\n");
+2

;)

(Win32 ) :

\\ \\ \\

\r\n\n .

+1

All Articles