文件读写完之后一定要close()
读文件:
string dataAdress = ConfigurationManager.AppSettings["DataAddress"];//文件路径
FileStream fs = new FileStream(dataAdress, FileMode.Open, FileAccess.Read);
StreamReader m_streamReader = new StreamReader(fs);
m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
string strLine = m_streamReader.ReadToEnd();
//一定记得close()
m_streamReader.Close();
fs.Close();
string path = GetPath("DataWeekAddress");
FileStream filestream = new FileStream(path, FileMode.Open, FileAccess.Read);
byte[] content = new byte[filestream.Length];
filestream.Read(content, 0, content.Length);
string strLine = Encoding.UTF8.GetString(content);
filestream.Close();
return Json(strLine);
写入文件:
string str=“asp.net”;
FileStream fs = new FileStream(@"D:\excise\ex2\ex2\ex2\attendence/WorkingTime.data", FileMode.Open,FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
fs.SetLength(0);//首先把文件清空
sw.Write(str);
sw.Close();
fs.Close();