码迷,mamicode.com
首页 > Windows程序 > 详细

C#读写txt文件

时间:2017-05-15 22:24:25      阅读:301      评论:0      收藏:0      [点我收藏+]

标签:file   color   bsp   代码   efault   default   ons   方式   test   

最简单的读写txt文件方式

引用:

using System.IO;
using System.Text;

代码:

class Program
    {
        static void Main(string[] args)
        {
            string path ="test.txt";
            Write(path);
            Console.WriteLine(Read(path));
            Console.ReadKey();
        }

        public static void Write(string path)
        {
            using (FileStream fs = new FileStream(path, FileMode.Append))
            {
                StreamWriter sw = new StreamWriter(fs);
                sw.WriteLine("Hello World!!!!");
                sw.WriteLine("Hello World!!!!");
                sw.Flush();
                Console.WriteLine("写入成功!");
            }
        }

        public static string Read(string path)
        {
            StreamReader sr = new StreamReader(path, Encoding.Default);
            return sr.ReadToEnd();
        }
    }

本程序为控制台程序,创建的test.txt文件在“\bin\Debug”目录下。

 

C#读写txt文件

标签:file   color   bsp   代码   efault   default   ons   方式   test   

原文地址:http://www.cnblogs.com/xbblogs/p/6858354.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!