File Directory FileStream 等的练习

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;
using System.Diagnostics;
using System.Threading;

namespace FileTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string strsw = "";
        long starttime;
        long endtime;

        private void Form1_Load(object sender, EventArgs e)
        {

        }


        //创建文件
        private void BtnCreate_Click(object sender, EventArgs e)
        {
            StreamWriter sw = new StreamWriter("test1.txt");

            Stopwatch watch = new Stopwatch();  //测试写文件所需时间  单位:ms  毫秒

            watch.Start();

            for (int i = 10000000; i >= 0; i--)
            {
                strsw = "" + i + "";
                sw.WriteLine(strsw);
            }

            watch.Stop();
            sw.Close();
            lbStart.Text = "用时:" + watch.ElapsedMilliseconds.ToString() + "ms";
            MessageBox.Show("创建文件完成,用时:" + watch.ElapsedMilliseconds + "ms");

        }


        //读取文件
        private void BtnReader_Click(object sender, EventArgs e)
        {
            richTextBox1.Text = "";
            string str = string.Empty;
            StreamReader sr = new StreamReader("test1.txt");
            Stopwatch watch = new Stopwatch();  //测试写文件所需时间  单位:ms  毫秒

            watch.Start();

            richTextBox1.Text += sr.ReadToEnd();

            watch.Stop();
            sr.Close();
            MessageBox.Show("读取完成,用时:" + watch.ElapsedMilliseconds + "ms");

        }



        private void Copy_Click(object sender, EventArgs e)
        {
            //通过file.copy()复制文件 path2必须包含完整路径和文件名
            string path = Application.StartupPath + @"\test.txt";
            string path2 = Application.StartupPath + @"\temp";
            Directory.CreateDirectory(path2);
            path2 += @"\test.txt";
            File.Copy(path, path2, true);



        }

        //删除文件
        private void BtnDel_Click(object sender, EventArgs e)
        {
            File.Delete(Application.StartupPath + @"\test.txt");
        }

        //目录下的文件名
        private void BtnReadFileName_Click(object sender, EventArgs e)
        {
            listBox1.Items.Add("文件名".PadRight(17) + "最后修改时间");
            string dir = Directory.GetCurrentDirectory();

            FileInfo[] fileinfo = new DirectoryInfo(dir).GetFiles("*.txt"); //只获取txt文件 不带参数获取所有扩展名文件

            for (int i = 0; i < fileinfo.Length; i++)
            {
                listBox1.Items.Add(fileinfo[i].Name.PadRight(20) + fileinfo[i].LastWriteTime);
            }

            //获取当前程序活动目录的父目录的父目录下的文件夹

            string dir2 = Directory.GetParent(Directory.GetParent(Directory.GetCurrentDirectory().ToString()).ToString()).ToString(); //获取目录
            DirectoryInfo[] dirinfo = new DirectoryInfo(dir2).GetDirectories();

            listBox1.Items.Add("--------------子目录-------------------");

            listBox1.Items.AddRange(dirinfo);

            //获取文件名
            listBox1.Items.Add("--------------文件名-----------");

            fileinfo = new DirectoryInfo(dir2).GetFiles();

            listBox1.Items.AddRange(fileinfo);
        }


        //剪贴文件
        private void BtnMoveDir_Click(object sender, EventArgs e)
        {
            //Move方法会删除原来的文件或者文件夹
            string path = @"c:\temp2";
            string path2 = @"c:\temp3";
            Directory.Move(path, path2);
        }

        //打开文件
        private void BtnOpenFile_Click(object sender, EventArgs e)
        {
            DialogResult result = openFileDialog1.ShowDialog();

            openFileDialog1.Title = "打开";

            if (result == DialogResult.OK)
            {
                textBox1.Text = openFileDialog1.FileName;

                Stream stream = openFileDialog1.OpenFile();

                StreamReader sr = new StreamReader(stream);

                richTextBox1.Text = sr.ReadToEnd();

                sr.Close();
            }


        }

        //保存文件
        private void BtnSaveFile_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Title = "保存";

            saveFileDialog1.RestoreDirectory = true;

            saveFileDialog1.Filter = "文本文件(*.txt)|*.txt|Word文档(*.doc)|*.doc|位图文件(*.bmp)|*.bmp";

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string filename = saveFileDialog1.FileName;

                string str = textBox2.Text;

                StreamWriter sw = new StreamWriter(filename, false, Encoding.Default);

                sw.WriteLine(str);

                sw.Close();
            }
        }

        private void BtnShowFolder_Click(object sender, EventArgs e)
        {

            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = folderBrowserDialog1.SelectedPath;

                listBox1.Items.Add("文件名".PadRight(17) + "最后修改时间");
                string dir = folderBrowserDialog1.SelectedPath;

                FileInfo[] fileinfo = new DirectoryInfo(dir).GetFiles();

                for (int i = 0; i < fileinfo.Length; i++)
                {
                    listBox1.Items.Add(fileinfo[i].Name.PadRight(20) + fileinfo[i].LastWriteTime);
                }
            }
        }

        private void BtnProbar_Click(object sender, EventArgs e)
        {

            new Thread(ProbarTest).Start();
            progressBar1.Value = 0;
            timer1.Interval = 2000;
            timer1.Start();
        }

        private void ProbarTest()
        {
            FileStream fr= null;
            FileStream fw = null;
            BinaryReader br = null;
            BinaryWriter bw = null;
            try
            {
                starttime = DateTime.Now.Second;
                byte[] buffer;

                fr = new FileStream(@"E:\Work\SYSTEM01.DBF", FileMode.Open, FileAccess.Read);
                int totalsize = (int)fr.Length;

                br = new BinaryReader(fr);
                buffer = br.ReadBytes(totalsize);


                fw = new FileStream(@"E:\Work\bak.DBF", FileMode.Create, FileAccess.Write);

                bw = new BinaryWriter(fw);

                bw.Write(buffer);

                endtime = DateTime.Now.Second;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                fr.Close();
                br.Close();
                fw.Close();
                bw.Close();
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (endtime - starttime > 0)
                progressBar1.Maximum = (int)(endtime - starttime);
            if (progressBar1.Value == progressBar1.Maximum)
            {
                progressBar1.PerformStep();
                timer1.Enabled = false;
                timer1.Dispose();
                return;
            }
            progressBar1.Value += 1;
        }


    }
}

转载于:https://www.cnblogs.com/ShuiMu/articles/2503994.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值