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

  /// <summary>
        /// 将output.config内容传到app.config
        /// </summary>
        string ReadString;
        //两个地址
        string path1 = @"D:\wcf\xml文件读写\Xml_WirteOrRead\Xml_WirteOrRead\output.config";
        string path2 = @"D:\wcf\xml文件读写\Xml_WirteOrRead\Xml_WirteOrRead\app.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# 对于文件的读取与写入!!个人做的一个小例子!

posted @ 2015-02-27 15:04  千年问心  阅读(181)  评论(0编辑  收藏  举报