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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        string fileInfo = string.Empty;
        public Form1()
        {
            InitializeComponent();
        }



        private void button2_Click(object sender, EventArgs e)
        {
            //选择文件夹
            if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                this.textBox1.Text = folderBrowserDialog1.SelectedPath;
            }
          
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string folderPath = this.textBox1.Text;
            DirectoryInfo di = new DirectoryInfo(folderPath);
            GetFolder(di);
            using (StreamWriter sw = new StreamWriter("d:\\a.txt", true))
            {
                sw.Write(fileInfo);
                sw.Flush();
            }

        }

        private void GetFolder(DirectoryInfo di)
        {
            GetFiles(di);
            foreach (DirectoryInfo dir in di.GetDirectories())
            {
                GetFolder(dir);
            }
        }
        private void GetFiles(DirectoryInfo di)
        {
            foreach (FileInfo dir in di.GetFiles())
            {
                fileInfo += dir.Name + "\r\n";
                Application.DoEvents();
            } 
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //选择文件
            string fileName = string.Empty;
            this.openFileDialog1.Filter = "Txt文件(*.txt)|*.txt|jpg文件(*.jpg)|*.jpg|gif文件(*.gif)|*.gif";
            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                fileName = this.openFileDialog1.FileName;
                this.textBox2.Text = fileName;

            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string filePath = this.textBox2.Text;
            if (filePath == string.Empty)
            {
                MessageBox.Show("请选择路径");
                return;
            }
            using (StreamReader sr = new StreamReader("d:\\a.txt", true))
            {
                
                string s = sr.ReadLine();
                while (s != null && s != string.Empty)
                {
                    MessageBox.Show(s);
                    s = sr.ReadLine();
                   
                }
                sr.Close();
            }
        }

  
    }
}

 

posted on 2013-09-16 23:02  鱼东鱼  阅读(225)  评论(0编辑  收藏  举报

导航