zoukankan      html  css  js  c++  java
  • 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# 对于文件的读取与写入!!个人做的一个小例子!

  • 相关阅读:
    使用国内镜像安装pyqt5
    python线程池 ThreadPoolExecutor 的用法及实战
    进程和线程、协程的区别
    python线程池实现
    python 多进程使用总结
    参与开源项目
    脑图——前端技术
    HTML中DTD使用小结
    浅谈面向对象——追溯法
    Dva.js 里面的几个概念
  • 原文地址:https://www.cnblogs.com/chcong/p/4303353.html
Copyright © 2011-2022 走看看