c#操作文本文件

 #region 操作文本文件
        /// <summary>
        /// 读取文件
        /// </summary>
        /// <param name="strPath">文件路径</param>
        /// <param name="strValue">文件内容</param>
        /// <returns></returns>
        public void readFile(string strPath, string strValue)
        {
            FileStream fsInfo = new FileStream(strPath, FileMode.Open, FileAccess.Read);
            StreamReader srInfo = new StreamReader(fsInfo, System.Text.Encoding.GetEncoding("GB2312"));
            srInfo.BaseStream.Seek(0, SeekOrigin.Begin);
            strValue = " ";
            string strLine = srInfo.ReadToEnd();
            while (strLine != null)
            {
                strValue += strLine + "\n";
                strLine = srInfo.ReadLine();
            }
            srInfo.Close();
        }

        /// <summary>
        /// 写入文件
        /// </summary>
        /// <param name="strPath">文件路径</param>
        /// <param name="strValue">文件内容</param>
        /// <returns></returns>
        public void writeFile(string strPath, string strValue)
        {
            FileStream fsInfo = new FileStream(strPath, FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter swInfo = new StreamWriter(fsInfo);
            swInfo.Flush();
            swInfo.BaseStream.Seek(0, SeekOrigin.Begin);
            swInfo.Write(strValue);

            swInfo.Flush();
            swInfo.Close();
        }
        #endregion

 

c#操作文本文件

上一篇:工控随笔_C#连接PLC_之_C#入门_05_类_01


下一篇:QT5.x应用在Mac OS X和Windows平台的发布过程