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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM