C# 文件读取方法,自己写的例子,保存一下,备用

  /// <summary>
        /// 将output.config内容传到app.config
        /// </summary>
        string ReadString;
        //两个地址
        string path1 = @"D:wcfxml文件读写Xml_WirteOrReadXml_WirteOrReadoutput.config";
        string path2 = @"D:wcfxml文件读写Xml_WirteOrReadXml_WirteOrReadapp.config"; /// <summary>
      /// 读文件
      /// </summary>
      /// <param name="path"></param>
        public void ReadFile(string path)
        {
            try
            {
                FileStream afile = new FileStream(path, FileMode.Open);
                StreamReader sread = new StreamReader(afile);
                //ReadString = sread.ReadLine();
                ReadString = sread.ReadToEnd();               
                sread.Close();
                afile.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// 写文件
        /// </summary>
        public void WriteFile()
        {
            try 
            {
                FileStream afile = new FileStream(path2, FileMode.OpenOrCreate);
                StreamWriter swrite = new StreamWriter(afile);
                if (!string.IsNullOrEmpty(ReadString))
                { 
                    swrite.Write(ReadString);
                   // swrite.WriteLine(ReadString);
                }                
                swrite.Close();
                afile.Close();            
        }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

C# 对于文件的读取与写入!!个人做的一个小例子!

原文地址:https://www.cnblogs.com/chcong/p/4303353.html