读写文件
怎么才读取一个文件中的内容哪?那位老兄可以给出代码。
------解决方案--------------------参考帮助里面的StreamReader类
------解决方案--------------------提示什么错误啊?
我一般不是这样写的
------解决方案--------------------//读取文件
private static void ReadFile()
{
using(FileStream fs = new FileStream( "d:\\Service.Txt ",FileMode.Open,FileAccess.Read))
{
StreamReader sr = new StreamReader(fs,Encoding.Default);
Console.WriteLine(sr.ReadToEnd());
sr.Close();
}
}
//写文件
private static void WriteFile()
{
StringBuilder sb = new StringBuilder();
using(FileStream fs = new FileStream( "c:\\CreateText.txt ",FileMode.Create,FileAccess.Write))
{
StreamWriter sr = new StreamWriter(fs);
for(int i=0;i <10000;i++)
sb.Append(i.ToString( "0000 ")+ ", ");
sr.Write(sb.ToString(0,sb.Length-1));
sr.Close();
}
}
------解决方案--------------------试试这样写
StreamReader strm = new StreamReader(new FileStream(@ "c:\cj.ini ", FileMode.Open));
string s;
s = strm.ReadLine();
while (s != null)
{
listBox1.Items.Add(s);
s = strm.ReadLine();
}
strm.Close();