C# 读写文本文件

/// <summary>
        /// 读取txt文件
        /// </summary>
        /// <param name="path"></param>
        public static string Read(string path)
        {
            StreamReader sr = new StreamReader(path, Encoding.Default);
            return sr.ReadLine();
        }

        /// <summary>
        /// 写入txt文件
        /// </summary>
        /// <param name="path"></param>
        public static Boolean Write(string path,string str)
        {
            try
            {
                FileStream fs = new FileStream(path, FileMode.Create);
                StreamWriter sw = new StreamWriter(fs);
                //开始写入
                sw.Write(str);
                //清空缓冲区
                sw.Flush();
                //关闭流
                sw.Close();
                fs.Close();
                return true;
            }
            catch(Exception ex)
            {
                return false;
            }
        }


版权声明:本文为hao123_66原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。