因为代码比较简单,就直接贴代码了。c#
主要用到流的方式进行操做。code
/// <summary> /// 写数据到文件 /// </summary> /// <param name="path">路径</param> /// <param name="content">内容</param> /// <returns>成功 true 失败 false</returns> public static Boolean WriteFile(string path, string content) { try { FileStream fs = new FileStream(path, FileMode.Create); StreamWriter sw = new StreamWriter(fs); //开始写入 sw.Write(content); //清空缓冲区 sw.Flush(); //关闭流 sw.Close(); fs.Close(); return true; } catch (Exception e) { return false; } } /// <summary> /// 读文件 /// </summary> /// <param name="path">文件路径</param> /// <returns></returns> public static string ReadFile(string Path) { StreamReader sr = null; try { sr = new StreamReader(Path, System.Text.Encoding.GetEncoding("utf-8")); string content = sr.ReadToEnd().ToString(); sr.Close(); return content; } catch { sr.Close(); return null; } }