c#读入,写入文本文件

老师留下的作业最后一条题目用到了,就百度下找到了例子修改成两个函数子程序方便使用.

        //**********************************************
        //函数名:ReadFile
        //功能:把指定文本内的数据读出并返回
        //参数: FileName文件名
        //返回值:string
        //**********************************************
        public static string ReadFile(string FileName)
        {
            StreamReader srReadFile = new StreamReader(FileName,Encoding.Default);
            string strReadLine = srReadFile.ReadToEnd(); //读取全部数据
            srReadFile.Close();
            return strReadLine; 
        }
        
        //**********************************************
        //函数名:MakeFile
        //功能:往指定文本中写入数据
        //参数: FileName文件名 Content写入内容
        //返回值:void
        //**********************************************
        public static void MakeFile(string FileName,string Content)
        {
            System.IO.StreamWriter  sw = new StreamWriter(FileName,false,Encoding.GetEncoding("gb2312"));
            try
            {
                sw.Write(Content);
                sw.Flush();
            }
            finally
            {
                if ( sw != null )
                    sw.Close();
            }  
        }
原文地址:https://www.cnblogs.com/Wzqa/p/2823258.html