控制台读写文件。

namespace ReadLineWriteFile
{
class Program
{
static void Main(string[] args)
{
List<string> listName = new List<string>();
do
{
Console.WriteLine("等待输入(q结束)");
string strname = Console.ReadLine();
if (strname.ToLower() == "q")
{
break;
}
listName.Add(strname);
} while (true);
Read(listName);
Console.WriteLine("OK");
Console.ReadKey();
}
static void Read(List<string> list)
{
for (int i = 0; i < list.Count; i++)
{
Write("test.txt", list[i]);
}
}
static void Write(string path, string str)
{
using (FileStream fs = new FileStream(path, FileMode.Append))
{
using (StreamWriter sr = new StreamWriter(fs,Encoding.Default))
{
sr.WriteLine(str);
}
}
}
}
}

原文地址:https://www.cnblogs.com/wrnsweet/p/6177879.html