从txt文件读取时在文本框中添加换行符

Adding line breaks in text box when reading from txt file

我正在尝试将来自多个文本框、组合框甚至 dgv 的内容保存到一个 txt 文件中,以便用户稍后重新打开。一些文本框是多行的,所以我使用当前代码来保存内容:

        private void label31_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                using (StreamWriter write = new StreamWriter(File.Create(sfd.FileName)))
                {
                    write.WriteLine(date.Text);
                    write.WriteLine(compName.Text);
                    write.WriteLine(jobLocation.Text);
                    write.WriteLine(county.Text);
                    write.WriteLine(titleWork.Text);
                    write.WriteLine(lineVoltage.Text);
                    write.WriteLine(woNum.Text);
                    write.WriteLine(ordBy.Text);
                    write.WriteLine(super.Text);
                    write.WriteLine(area.Text);
                    write.WriteLine(bidOrTM.Text);
                    write.WriteLine(proMan.Text);
                    write.WriteLine(workSchDay.Text);
                    write.WriteLine(hoursNearest.Text);
                    write.WriteLine(perDiem.Text);
                    write.WriteLine(perDiemPerDay.Text);
                    write.WriteLine(rock.Text);
                    write.WriteLine(hotWork.Text);
                    string mltb1 = jobStory.Text.Replace(Environment.NewLine, @" \n ");
                    write.WriteLine(mltb1);
                    write.WriteLine(dtPurpose.Text);
                    write.WriteLine(gpsLat.Text);
                    write.WriteLine(gpsLong.Text);
                    write.WriteLine(city.Text);
                    write.WriteLine(majIntersect.Text);
                    write.WriteLine(pocPhone.Text);
                    string mltb2 = markInstr.Text.Replace(Environment.NewLine, @" \n ");
                    write.WriteLine(mltb2);
                    write.WriteLine(utilityComp.Text);
                    string mltb3 = utilityWork.Text.Replace(Environment.NewLine, @" \n ");
                    write.WriteLine(mltb3);
                    write.WriteLine(printedDate.Text);
                    write.WriteLine(reqNum.Text);

                    string sLine = "";

                    for (int r = 0; r <= stkSheetdgv.Rows.Count - 1; r++)
                    {
                        for (int c = 0; c <= stkSheetdgv.Columns.Count - 1; c++)
                        {
                            sLine = sLine + stkSheetdgv.Rows[r].Cells[c].Value;
                            if (c != stkSheetdgv.Columns.Count - 1)
                            {
                                sLine = sLine + ", ";
                            }
                        }
                        write.WriteLine(sLine);
                        sLine = "";
                    }
                    write.Close();
                }
            }
        }

这是我在打开 txt 文件时使用的代码:

        private void label30_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                StreamReader read = new StreamReader(File.OpenRead(ofd.FileName));
                date.Text = read.ReadLine();
                compName.Text = read.ReadLine();
                jobLocation.Text = read.ReadLine();
                county.Text = read.ReadLine();
                titleWork.Text = read.ReadLine();
                lineVoltage.Text = read.ReadLine();
                woNum.Text = read.ReadLine();
                ordBy.Text = read.ReadLine();
                super.Text = read.ReadLine();
                area.Text = read.ReadLine();
                bidOrTM.Text = read.ReadLine();
                proMan.Text = read.ReadLine();
                workSchDay.Text = read.ReadLine();
                hoursNearest.Text = read.ReadLine();
                perDiem.Text = read.ReadLine();
                perDiemPerDay.Text = read.ReadLine();
                rock.Text = read.ReadLine();
                hotWork.Text = read.ReadLine();
                jobStory.Text = read.ReadLine();
                dtPurpose.Text = read.ReadLine();
                gpsLat.Text = read.ReadLine();
                gpsLong.Text = read.ReadLine();
                city.Text = read.ReadLine();
                majIntersect.Text = read.ReadLine();
                pocPhone.Text = read.ReadLine();
                markInstr.Text = read.ReadLine();
                utilityComp.Text = read.ReadLine();
                utilityWork.Text = read.ReadLine();
                printedDate.Text = read.ReadLine();
                reqNum.Text = read.ReadLine();

                string[] lines = File.ReadAllLines(ofd.FileName);
                string[] values;

                for (int i = 30; i < lines.Length; i++)
                {
                    values = lines[i].ToString().Split(',');
                    string[] row = new string[values.Length];

                    for (int j = 0; j < values.Length; j++)
                    {
                        row[j] = values[j].Trim();
                    }
                    stkSheetdgv.Rows.Add(row);

                    read.Close();
                    read.Dispose();

                }
                
            }
        }

我 运行 遇到的问题是,当多行填充的文本框的文本值显示如下时:

值\n值

当我需要显示时:

价值

价值

我确定这是一个简单的解决方案,我只是无知。

感谢您的帮助。

正如@Magnetron 所说,当您写入文件时,您正在用'\n'

替换系统换行符

以这几行为例:

tring mltb1 = jobStory.Text.Replace(Environment.NewLine, @" \n "); write.WriteLine(mltb1);

当您这样做时,您文件中的文本实际上是使用字符“\n”。当您读回它时,您需要通过对可能 multi-line 的任何内容执行 Text.Replace(@"\n", Environment.NewLine) 来撤消该操作。在读取之前生成的文件时需要进行这种反转。