【问题标题】:New line in a string is not works with text file字符串中的新行不适用于文本文件
【发布时间】:2023-01-02 16:42:38
【问题描述】:

我有一个将消息写入文本文件的功能。但是当传递带有“Environment.NewLine”的字符串时,它不会将新行写入文本文件。相反,它会写入“\r\n”。 如何纠正这个?我尝试过使用“\n”而不是“Environment.NewLine”。新线路仍然没有到来。 只有在将带有换行符的字符串传递给函数时才会发生此问题。像变量“消息”

string message= "First "+Environment.NewLine+" message.";
LogTheDetails(message);

      public static void LogTheDetails(String message)
                {
          string path = AppDomain.CurrentDomain.BaseDirectory + "\\logs";       
          var directory = new DirectoryInfo(path);
             if (directory.Exists == false)
                        {
                            directory.Create();
                        }
        
                        string FilePath = path + "/" +  currentdate + ".txt";
                        if (!File.Exists(FilePath)) //FILE WRITING FIRST TIME
                        {
                            File.Create(FilePath).Dispose();
                            using (TextWriter tw = new StreamWriter(FilePath))
                            {
              tw.WriteLine("============================================================\n --Logs--");
                            }
                        }
                        else if (File.Exists(FilePath))//IF FILE ALREADY EXIST - APPEND LINES
                        {
                            string testString= "testString"+ Environment.NewLine+ "WithNewLine";
        File.AppendAllText(FilePath, "\n============================================================\n");
                            File.AppendAllText(FilePath, message);
                            File.AppendAllText(FilePath, testString);  
        File.AppendAllText(FilePath, "\n============================================================\n");
                        }
        }

输出

============================================================
--Logs--
============================================================
 First \r\n message.
testString
WithNewLine
============================================================

预期输出:

 ============================================================
    --Logs--
    ============================================================
    First
    message.
    testString
    WithNewLine
    ============================================================

【问题讨论】:

  • 你的问题不清楚。在什么情况下你想写\n?哪一行应该为它写一个新行?我也看不到你写的任何一行First
  • @viveknuna string message= "第一条"+Environment.NewLine+"消息。";
  • 您在哪里以及如何调用该函数? - 的:代码很难阅读,因为你的格式太乱了!
  • 每当 Environment.NewLine 出现时,它应该换行。现在输出显示为 '/r/n'
  • @TaW 请现在检查我的代码

标签: c# string file utf-8 system.io.file


【解决方案1】:

下面的一段代码给出了您正在寻找的输出,我在 .net 5 控制台应用程序中尝试过。

using System;
using System.IO;

namespace ConsoleApp4
{
class Program
{
    static string message = "First " + Environment.NewLine + " message.";

    static void Main(string[] args)
    {
        LogTheDetails(message);
        Console.WriteLine("Hello World!");
    }



    public static void LogTheDetails(string message)
    {
        string path = AppDomain.CurrentDomain.BaseDirectory + "\logs";
        var directory = new DirectoryInfo(path);
        if (directory.Exists == false)
        {
            directory.Create();
        }

        string currentdate = "test";
        string FilePath = path + "/" + currentdate + ".txt";
        if (!File.Exists(FilePath)) //FILE WRITING FIRST TIME
        {
            File.Create(FilePath).Dispose();
            using (TextWriter tw = new StreamWriter(FilePath))
            {
         
tw.WriteLine("========================================================
 
--Logs--");       
      
            }
        }
        else if (File.Exists(FilePath))//IF FILE ALREADY EXIST - APPEND 
 LINES
        {
            string testString = Environment.NewLine + "testString" + 
Environment.NewLine + "WithNewLine";
            File.AppendAllText(FilePath, 
"
============================================================
");
            File.AppendAllText(FilePath, message);
            File.AppendAllText(FilePath, testString);
            File.AppendAllText(FilePath, 
"
============================================================
");
        }
    }
}
}

【讨论】:

  • 它和我的代码一样吗?它正在给予 什么时候打开文本文件
【解决方案2】:

message 变量包含 "First \r\n message.",而不是 "First message."

"\r\n" 是字符串 (四个字符):+ r + + n

" " 是字符串(两个字符):[Return chariot] + [New Line]


您可以使用 Regex.Unescape 方法取消转义字符串文字:

public static void LogTheDetails(string message)
{
    message = Regex.Unescape(message);
    ...
}

如果你只想逃避“ “,你可以只是String.Replace

public static void LogTheDetails(string message)
{
    message = message.Replace("
", Environment.NewLine);
    ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-27
    • 1970-01-01
    • 1970-01-01
    • 2014-07-13
    • 1970-01-01
    • 1970-01-01
    • 2012-03-10
    相关资源
    最近更新 更多