向文件中写数据,读数据

来源:互联网 发布:qq同步助手 for mac 编辑:程序博客网 时间:2024/06/11 17:25
  1.          private void WriteFile(string strPath,string strWrite)
  2.         {
  3.             FileStream fs = new FileStream(strPath, FileMode.Create, FileAccess.Write);
  4.             StreamWriter sw = new StreamWriter(fs);
  5.             sw.WriteLine(strWrite);
  6.             sw.Close();
  7.         }
  8.         private string ReadFile(string strPath)
  9.         {
  10.             string strRead = string.Empty;
  11.             FileStream fs = new FileStream(strPath, FileMode.Open, FileAccess.Read);
  12.             StreamReader sr = new StreamReader(fs);
  13.             if (sr.Peek() != -1)
  14.             {
  15.                 strRead = sr.ReadLine();
  16.             }
  17.             sr.Close();
  18.             return strRead;
  19.         }