对文件的处理

一:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

namespace CG27_winform3

{

    public partial class Form2 : Form

    {

        public Form2()

        {

            InitializeComponent();

        }

      //   打开文件对话框,将文件路径显示在文本框中

        private void button1_Click(object sender, EventArgs e)

        {

            //点击打开按钮所做的操作

            OpenFileDialog ofd = new OpenFileDialog();

            ofd.Filter = "图片|*.jpg;*.bmp;*.png;*.gif|全部文件|*.*"; //文过滤器件,选择需要的文件类型 

            ofd.Multiselect = true;        //允许选中多个文件

            DialogResult dr = ofd.ShowDialog();    //dr接收选中的条件(确定、取消)

            if (dr == System.Windows.Forms.DialogResult.OK) //选择确定键

            {

                //textBox1.Text = ofd.FileName;

                textBox1.Text = ""; //textBox1显示路径的文本框

                foreach (string item in ofd.FileNames) //选择多个文件时,循环输出他们的路径

                {

                    textBox1.Text += item + ";";

                }

            }

        }

        private void button2_Click(object sender, EventArgs e)

        {

            //点击保存按钮所做的操作,不会真正的保存文件,只会显示出保存的路径

           SaveFileDialog ofd = new SaveFileDialog();

            ofd.Filter = "图片|*.jpg;*.bmp;*.png;*.gif|全部文件|*.*";

            DialogResult dr = ofd.ShowDialog();

            if (dr == System.Windows.Forms.DialogResult.OK)

            {

                MessageBox.Show(ofd.FileName);

            }

        }

    }

}

 二:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.IO;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

namespace CG27_winform3

{

    public partial class Form3 : Form

    {

        public Form3()

        {

            InitializeComponent();

        }

        //对文件内容的操作

        private void 加载ToolStripMenuItem_Click(object sender, EventArgs e)

        {

            //当点击加载按钮时,将会将“E:笔记笔记2.txt”路径中的数据加载到窗体的文本框中

            //1.建立文件流

            FileStream fs = new FileStream(@"E:笔记笔记2.txt", FileMode.Open);

            //2.建立流读取器

            StreamReader reader = new StreamReader(fs, Encoding.Default);

            //3.读取数据

            //reader.EndOfStream;//bool类型,表示是否读取完成

            //reader.ReadLine();//返回字符串类型,读取一行数据,并将光标移动到下一行

            txt.Text = reader.ReadToEnd();

            //4.关闭读取器

            //reader.Close();//关闭,关闭后可再次打开使用

            reader.Dispose();//销毁,彻底销毁对象资源,无法再次使用

            //5.关闭文件流

            //fs.Close();

            fs.Dispose();

        }

        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)

        {

            //点击保存按钮时,将会保存“E:笔记笔记2”文件,重新命名为笔记2副本,保存后的新路径为“”

            //1.建立文件流

            FileStream fs = new FileStream(@"E:笔记笔记2副本.txt", FileMode.Create);

            //2.建立流写入器

            StreamWriter writer = new StreamWriter(fs, Encoding.Default);

            //3.写入数据

            writer.Write(txt.Text);

            //writer.WriteLine(txt.Text);

            //4.关闭写入器

            //writer.Close();//关闭,关闭后可再次打开使用

            writer.Dispose();//销毁,彻底销毁对象资源,无法再次使用

            //5.关闭文件流

            //fs.Close();

            fs.Dispose();

            MessageBox.Show("保存成功");

        }

    }

}

 三:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.IO;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

namespace CG27_winform3

{

    public partial class Form4 : Form

    {

        public Form4()

        {

            InitializeComponent();

        }

        //二.文件信息

        //打开一个文件。查询这个文件夹的信息:

        //文件名,所在文件夹,创建日期,修改日期,文件大小

        private void button1_Click(object sender, EventArgs e)

        {

            OpenFileDialog ofd = new OpenFileDialog();

            DialogResult dr = ofd.ShowDialog();

            if (dr == System.Windows.Forms.DialogResult.OK)

            {

                string path = ofd.FileName;

                FileInfo fi = new FileInfo(path);

                string str = string.Format("文件名:{0} 所在文件夹:{1} 创建日期:{2} 修改日期:{3} 文件大小:{4}KB",

                    fi.Name, fi.DirectoryName, fi.CreationTime.ToString("yyyy-MM-dd HH:mm:ss"),

                    fi.LastWriteTime, fi.Length / 1024.0);

                lblInfo.Text = str;

            }

        }

    }

}

 四:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.IO;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

namespace CG27_winform3

{

    public partial class Form5 : Form

    {

        public Form5()

        {

            InitializeComponent();

        }

        //三.文件和操作目录的

        private void Form5_Load(object sender, EventArgs e)

        {

            //得到f盘文下的文件夹

            string[] paths = Directory.GetDirectories(@"f:");

            foreach (string item in paths)

            {

                cboDirectories.Items.Add(item);

            }

            cboDirectories.SelectedIndex = 0;

        }

        private void cboDirectories_SelectedIndexChanged(object sender, EventArgs e)

        {   //得到在上一条件中选中的文件下的子文件

           

            string path = cboDirectories.Text;//得到当前文件夹路径

            string[] files = Directory.GetFiles(path);

            cboFiles.Items.Clear(); //当f盘中的选中文件改变时,下拉列表中的相应的子文件也要改变

            foreach (string item in files)

            {

                cboFiles.Items.Add(item);//cboFiles为文本框的名字

            }

        }

        private void button1_Click(object sender, EventArgs e)

        {//删除选中文件

           File.Delete(cboFiles.Text);

            MessageBox.Show("删除成功");

        }

        private void button2_Click(object sender, EventArgs e)

        {

            //移动选中文件到另一个文件夹中

            FileInfo fi = new FileInfo(cboFiles.Text);

            File.Move(cboFiles.Text, @"d:" + fi.Name);

            MessageBox.Show("移动成功");

        }

        private void button3_Click(object sender, EventArgs e)

        {

            //复制选中文件到另一个文件路径中

            FileInfo fi = new FileInfo(cboFiles.Text);

            File.Copy(cboFiles.Text, @"d:" + fi.Name);

            MessageBox.Show("复制成功");

       

            //File.Exists(文件路径)  返回一个bool类型,判断给定路径上是否存在文件

        }

    }

}

 五:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.IO;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

namespace _2015_7_3_树形

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)

        {

            //当窗体加载时,显示我的电脑

            TreeNode tn = new TreeNode();

            tn.Text = "我的电脑";

            //将这个节点添加到树形中去

            tvwenjianjia.Nodes.Add(tn);

            //获取计算机上的盘

            DriveInfo[] pan = DriveInfo.GetDrives();

            // 将这些盘添加到树形中去

            foreach (DriveInfo item in pan)

            {

                //判断,已经准备好的盘

                if (item.IsReady)

                {

                    //如果准备好就放在树形中去                  

                    TreeNode tn1 = new TreeNode();

                    tn1.Text = item.VolumeLabel + " " + item.Name;

                    tn1.Tag = item.Name;

                    tn.Nodes.Add(tn1);//将这些盘添加到“我的电脑的树形下”

                }

            }

        }

        //添加节点的方法,传入一个节点

        public void addjiedian(TreeNode Node)

        {    //文件的名称

            string path = Node.Tag.ToString();

            //找到文件下的子文件的名称

            string[] ziwenjian = Directory.GetDirectories(path);

                //循环得到选中盘的子文件,将该文件添加到树形中去

                foreach (string item in ziwenjian)

                {

                    DirectoryInfo di = new DirectoryInfo(item);

                    if(di.Attributes==FileAttributes.Directory){

                        TreeNode tn1 = new TreeNode();

                        tn1.Text = di.Name ;

                        tn1.Tag = item;

                        Node.Nodes.Add(tn1);

                    }

                }

         }

        private void tvwenjianjia_AfterExpand(object sender, TreeViewEventArgs e)

        {

            foreach (TreeNode item in e.Node.Nodes )//循环展开的节点

            {

                if(item.Nodes.Count==0){//如果打开的这个节点没有节点,就循环找出节点。

                    addjiedian(item);

                }                

            }           

        }

    }

}

原文地址:https://www.cnblogs.com/yaoxiaodan/p/4641086.html