如何将 StreamReader 转换为字符串?
我更改了代码,以便可以以只读方式打开文件。现在我在使用 File.WriteAllText
时遇到问题,因为我的 FileStream
和 StreamReader
未转换为字符串。
这是我的代码:
static void Main(string[] args)
{
string inputPath = @"C:\Documents and Settings\All Users\Application Data\"
+ @"Microsoft\Windows NT\MSFax\ActivityLog\OutboxLOG.txt";
string outputPath = @"C:\FAXLOG\OutboxLOG.txt";
var fs = new FileStream(inputPath, FileMode.Open, FileAccess.Read,
FileShare.ReadWrite | FileShare.Delete);
string content = new StreamReader(fs, Encoding.Unicode);
// string content = File.ReadAllText(inputPath, Encoding.Unicode);
File.WriteAllText(outputPath, content, Encoding.UTF8);
}
I altered my code so I could open a file as read only. Now I am having trouble using File.WriteAllText
because my FileStream
and StreamReader
are not converted to a string.
This is my code:
static void Main(string[] args)
{
string inputPath = @"C:\Documents and Settings\All Users\Application Data\"
+ @"Microsoft\Windows NT\MSFax\ActivityLog\OutboxLOG.txt";
string outputPath = @"C:\FAXLOG\OutboxLOG.txt";
var fs = new FileStream(inputPath, FileMode.Open, FileAccess.Read,
FileShare.ReadWrite | FileShare.Delete);
string content = new StreamReader(fs, Encoding.Unicode);
// string content = File.ReadAllText(inputPath, Encoding.Unicode);
File.WriteAllText(outputPath, content, Encoding.UTF8);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 StreamReader 的 ReadToEnd() 方法:
当然,访问后关闭 StreamReader 很重要。因此,正如 keyboardP 和其他人所建议的,
using
语句是有意义的。use the ReadToEnd() method of StreamReader:
It is, of course, important to close the StreamReader after access. Therefore, a
using
statement makes sense, as suggested by keyboardP and others.使用 StreamReader.ReadToEnd() 方法。
Use
StreamReader.ReadToEnd()
method.