C#对于文件的读写

时间:2022-05-10 06:27:55

C#文件的读写操作

转载请注明出处 http://www.cnblogs.com/Huerye/

   /// <summary>
        /// 写入txt文件
        /// </summary>
        /// <param name="txt">写入文本</param>
        private void TxtCreate(string txt)
        {
            FileStream aFile = new FileStream(@"C:\Users\Administrator\Desktop\config.txt", FileMode.OpenOrCreate);//文件流--可以生成任何后缀格式的文件
            StreamWriter sw = new StreamWriter(aFile);//写入流
            sw.WriteLine(txt);
            sw.Close();//关闭写入流
        }

        /// <summary>
        /// 读取文件
        /// </summary>
        /// <returns>文本内容</returns>
        private string TxtOpen()
        {
            FileStream aFile = new FileStream(System.Windows.Forms.Application.StartupPath+@"\config.ini", FileMode.Open);//文件流
            StreamReader sr = new StreamReader(aFile);//读取流
            string result = sr.ReadToEnd();//读取所有内容
            sr.Close();//关闭
            return result;
        }