c#的文件流的小范例

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace wht
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string path = @"c:\temp\MyTest.txt";
           
                if (File.Exists(path))
                {
                    File.Delete(path);
                }

                using (StreamWriter sw = new StreamWriter(path))
                {
                    sw.WriteLine("This");
                    sw.WriteLine("is some textvbbnvnvncfdh");
                    sw.WriteLine("to test");
                    sw.WriteLine("Reading");
                }

                using (StreamReader sr = new StreamReader(path))
                {
                    string tt = "";
                    while (sr.Peek() >= 0)
                    {
                       // Console.WriteLine(sr.ReadLine());
                       tt += sr.ReadLine() + "\n";
                    }
                    MessageBox.Show(tt);
                }
              
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

    }
}

原文地址:https://www.cnblogs.com/softimagewht/p/2235247.html