更新时间:2023-11-07 20:06:16
如何:
How about:
string filepath = ...;
string text = File.ReadAllText(filepath);
...
File.WriteAllText(filepath, text);
这是您要的吗?
文本在读取和写入时已经包含新行-不需要特殊处理,对吗?
Is this what you ask for?
The text already contains the new lines, while reading and writing - no special handling needed, right?
除了提到解决方案3之外,您还可以尝试使用以下方法之一,
In addition to the Solution 3 mention, you could try using one of the following methods,
WriteAllLines(String, IEnumerable(Of String))
WriteAllLines(String, String())
WriteAllLines(String, IEnumerable(Of String), Encoding)
WriteAllLines(String, String(), Encoding)
WriteAllText(String, String)
WriteAllText(String, String, Encoding)
更多详细信息文件类 [
More details File Class[^]
Hope it helps :)
其他答案的重要补充:请勿使用显式的
您永远不要使用"\ r \ n",因为使用立即常量是不好的.这不是便携式的.正确的事情是使用静态属性System.Environment.NewLine
.
(顺便说一句,实际上您应该尽量避免使用所有这些立即常量,除了0、1,null之类的东西.即使字符串"是坏的;您也应该使用string.Empty
.)
请参阅: http://msdn.microsoft.com/en-us/library/system .environment.newline.aspx [ ^ ].—SA
An important addition to other answers: don''t use explicit
You should never use "\r\n" — using immediate constants is bad; and this is not portable. Right thing is using static propertySystem.Environment.NewLine
.
(By the way, you really should avoid all those immediate constants by all means, except something like 0, 1, null. Even the string "" is bad; you should usestring.Empty
.)
Please see: http://msdn.microsoft.com/en-us/library/system.environment.newline.aspx[^].—SA