我要读取和写入文本文件,主要是现有的方法读取中文时会显示乱码private string file_get_contents(string path)
{
string a="";
StreamReader sr = File.OpenText(path);
string s = "";
while ((s = sr.ReadLine()) != null)
{
a += s;
}
return a;
}另外,请问哪里可以查到常用C#操作的代码?谢谢!
{
string a="";
StreamReader sr = File.OpenText(path);
string s = "";
while ((s = sr.ReadLine()) != null)
{
a += s;
}
return a;
}另外,请问哪里可以查到常用C#操作的代码?谢谢!
using (StreamReader sr = new StreamReader(path,System.Text.Encoding.GetEncoding("GB2312")))
{
str=sr.ReadToEnd();
}
{
string a="";
StreamReader sr = File.OpenText(path);
using (StreamReader sr = new StreamReader(path,System.Text.Encoding.GetEncoding ("GB2312")))
{
string s = "";
while ((s = sr.ReadLine()) != null)
{
a += s;
}
}
return a;
}
{
StringBuilder s=new StringBuilder();
using (StreamReader sr = new StreamReader(path,System.Text.Encoding.GetEncoding ("GB2312")))
{
while (sr.ReadLine()!= null && sr.ReadLine().ToString()!="")
{
s.Append(sr.ReadLine());
}
}
return s.ToString();
}--------------
using System.Text;
using System.IO;
{
StreamReader sr = new StreamReader(path, System.Text.Encoding.GetEncoding("GB2312"));
return sr.ReadToEnd();
}
如果这样没有sr.Close()的话,系统会自动回收内存,是吧?
这里的using是什么意思?谢谢!