提问人:user2431727 提问时间:1/2/2023 最后编辑:user2431727 更新时间:1/2/2023 访问量:110
字符串中的换行符不适用于文本文件
New line in a string is not works with text file
问:
我有一个将消息写入文本文件的功能。但是,当使用“Environment.NewLine”传递字符串时,它不会向文本文件写入新行。相反,它写的是“\r\n”。 如何纠正这个问题?我尝试使用“\n”而不是“Environment.NewLine”。尽管如此,新生产线仍未到来。 只有当将带有换行符的字符串传递给函数时,才会发生此问题。像变量 'message'
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
============================================================
答:
0赞
karthik kasubha
1/2/2023
#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("========================================================\n
--Logs--");
}
}
else if (File.Exists(FilePath))//IF FILE ALREADY EXIST - APPEND
LINES
{
string testString = Environment.NewLine + "testString" +
Environment.NewLine + "WithNewLine";
File.AppendAllText(FilePath,
"\n============================================================\n");
File.AppendAllText(FilePath, message);
File.AppendAllText(FilePath, testString);
File.AppendAllText(FilePath,
"\n============================================================\n");
}
}
}
}
评论
0赞
user2431727
1/2/2023
它和我的代码一样,对吧?打开文本文件时,它给出\r\n
0赞
karthik kasubha
1/2/2023
是的,它几乎是相同的代码,正如我提到的,它对我来说工作正常。请创建一个新的控制台应用程序并粘贴代码。如果文件已经存在,请确保删除该文件,以确保从头开始。在您的情况下,这可能是一些兼容性问题。
0赞
AmigoJack
1/4/2023
如何包括您实际更改了某些内容以及为什么更改的评论?
评论
\n
First