我对 c# 和 .net 非常陌生,并试图理解它。
我正在使用如何读取特定文件夹中的所有文件并尝试在下面的代码中应用的解决方案。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace HowToCopyTextFiles
{
class Program
{
static void Main(string[] args)
{
StringBuilder sb = new StringBuilder();
foreach (string txtName in Directory.GetFiles(@"C:\Users\Environ ment\Desktop\newfolder","*.rtf"))
{
using (StreamReader sr = new StreamReader(txtName))
{
sb.Append(sr.ReadToEnd());
sb.AppendLine();
}
}
Console.Write(sb.ToString());
Console.ReadLine();
}
}
}
结果没问题,但在我的测试文件末尾显示环境名称。
像。
this is content of first file
this is content of second file
↑My environment full name ↑My
environment full name ↑My environment full name (Yes 3 times)
我正在使用cs-script,是不是因为这个?
使用 .txt 文件时,它工作正常。所以问题是如何正确打开 .rtf 文件作为文本流?