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# 对于文件的读取与写入!!个人做的一个小例子!

你可能感兴趣的:(文件读取)