FileInfo与DirectoryInfo

前篇写了 File类与Directory类,今天写 他的 实例类,嘿嘿

先上张图:

 

附上 静态类与实例类的区别:

Fileinfo类与DirectoryInfo类

//文件
File类是静态类,而FileInfo没有静态方法的。File类的静态方法在调用时要执行安全检查。
对于操作单一的文件,用静态方法快,但是如果对对象多种操作,用实例化方法,静态方法会每次都寻找文件,而实例化方法只是引用查找。

方法比较:
Exists方法
1.file类 如果读取文件的权限不够,返回false,如果文件不存在返回false
2.fileinfo类 如果文件不存在或输入的是目录返回false

Create方法
1.如果Create创建目录,路径为空或文件夹为只读那么会出现异常
2.对于fileinfo类的Create,默认情况是建立新文件为 读写访问权限的

Copy  CopyTo方法
1.File类 目标文件不能为目录  File.Copy(源文件,目标文件,是否覆盖)
2.FileInfo类 CopyTo          file.CopyTo(目标文件,是否覆盖)

Move  MoveTo方法
1.File类 在移动的时候,目标文件如果已存在,则异常  File.Move(源文件,目标文件)
2.FileInfo  file.MoveTo(目标文件)

Delete方法
1.File类 如果文件正在使用,则异常
2.同上

//目录
方法比较:
Exists方法
1.Directory类  绝对路径和相对路径(当前工作目录)  Directory.Exists(目录)
2.DirectoryInfo类  类似   directory.Exist()

Create方法
1.Directory类与DirectoryInfo类 都可以设置访问权限

Move MoveTo Delete方法
1.注意 可以选择 是否移除 子目录及文件

 

源码如下:

View Code
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 FileInfoAndDirectoryInfo
{
    public partial class File_Directroy_Info : Form
    {
        public File_Directroy_Info()
        {
            InitializeComponent();
        }
        
        private void File_Directroy_Info_Load(object sender, EventArgs e)
        {
            //textBox未输入 Button不可用

            btnShowInfo.Enabled = false;
            btnNewF.Enabled = false;
            btnShowInfo2.Enabled = false;
            btnNewD.Enabled = false;
            btnSelectD.Enabled = false;
            btnSelectD.Enabled = false;
            btnSelectF.Enabled = false;
            btnSelectAll.Enabled = false;

        }

        //TextBox1 事件
        private void txtPathFile_TextChanged(object sender, EventArgs e)
        {
            btnNewF.Enabled = true;
            btnShowInfo.Enabled = true;
        }

        //TextBox2 事件
        private void txtDirectory_TextChanged(object sender, EventArgs e)
        {
            btnShowInfo2.Enabled = true;
            btnNewD.Enabled = true;

        }
        //TextBox3 事件
        private void txtSelect_TextChanged(object sender, EventArgs e)
        {
            btnSelectD.Enabled = true;
            btnSelectF.Enabled = true;
            btnSelectAll.Enabled = true;

        }


        //新建文件 事件
        private void btnNewFile(object sender, EventArgs e)
        {
            lbShowInfo1.Items.Clear();
            if (txtPathFile.Text == string.Empty)
            {
                MessageBox.Show("不能为空");
                return;
            }
            else
            {
                FileInfo file = new FileInfo(txtPathFile.Text);
                if (file.Exists)
                {
                    MessageBox.Show("文件名存在!");
                    return;
                }
                else
                {
                    file.Create();
                    lbShowInfo1.Items.Add(string.Format("提示:{0}已创建",txtPathFile.Text));
                }
            }
        }

        //显示文件详细信息 事件
        private void btnShowInfo_Click(object sender, EventArgs e)
        {
            lbShowInfo1.Items.Clear();
            if (txtPathFile.Text == string.Empty)
            {
                MessageBox.Show("文件为空!");
                return;
            }
            else
            { 
                FileInfo file=new FileInfo(txtPathFile.Text);
                if (!file.Exists)
                {
                    DialogResult result=MessageBox.Show("文件不存在,是否创建该文件","提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Warning);
                    
                    if (result == DialogResult.OK)
                    {
                        file.Create();
                        lbShowInfo1.Items.Add(string.Format("提示:{0}已创建", txtPathFile.Text));
                        ShowInfo(txtPathFile.Text, lbShowInfo1);
                    }
                    else
                    {
                        return;
                    }
                }
                else
                {
                    ShowInfo(txtPathFile.Text, lbShowInfo1);
                }
            }
        }

        //定义文件信息显示方法
        private void ShowInfo(string filename,ListBox lb)
        {
            FileInfo file = new FileInfo(filename);
            string creatTime = "创建时间:"+file.CreationTime.ToString() ;
            string directory = "父目录:"+file.Directory.ToString();
            string exist = (file.Exists) ? "文件: 存在" : "文件: 不存在";
            string extension = "扩展名:"+file.Extension;
            string length = "字节:"+file.Length.ToString();
            string isonlyread = (file.IsReadOnly) ? "文件:只读" : "文件:读写";
            string name = "文件名:" + file.Name;
            string[] strs = { creatTime,directory,exist,extension,length,isonlyread,name};
            foreach (var n in strs)
            {
                lb.Items.Add(n);
            }

        }

        //新建目录 事件
        private void btnNewD_Click(object sender, EventArgs e)
        {
            lbShowInfo2.Items.Clear();
            if (txtDirectory.Text == string.Empty)
            {
                MessageBox.Show("目录为空!");
                return;
            }
            else
            {
                DirectoryInfo directory = new DirectoryInfo(txtDirectory.Text);
                if (directory.Exists)
                {
                    MessageBox.Show("目录名存在!");
                    return;
                }
                else
                {
                    directory.Create();
                    lbShowInfo2.Items.Add("提示: 目录已创建");
                }

            }
        }

        private void btnShowInfo2_Click(object sender, EventArgs e)
        {
            lbShowInfo2.Items.Clear();
            if (txtDirectory.Text == string.Empty)
            {
                MessageBox.Show("目录为空!");
                return;
            }
            else
            {
                DirectoryInfo directory = new DirectoryInfo(txtDirectory.Text);
                if (!directory.Exists)
                {
                    DialogResult result = MessageBox.Show("查询的目录不存在,是否新建该目录", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                    if (result == DialogResult.OK)
                    {
                        directory.Create();
                        lbShowInfo2.Items.Add("提示:目录已创建");
                        ShowInfo2(txtDirectory.Text, lbShowInfo2);
                    }
                    else
                    {
                        return;
                    }
                }

                else
                {
                    ShowInfo2(txtDirectory.Text, lbShowInfo2);
                }
            }
        }

        //显示信息的方法
        private void ShowInfo2(string directory, ListBox lb)
        { 
            DirectoryInfo dir=new DirectoryInfo(directory);
            string creatTime = "创建时间:"+dir.CreationTime.ToString();
            string exists=(dir.Exists)? "文件:存在" :"文件不存在";
            string length = "父目录:" + dir.Parent.ToString();
            string root = "根路径:" + dir.Root.ToString();
            string[] strs = { creatTime,exists,length,root};
            foreach(var n in strs)
            {
                lb.Items.Add(n);
            }
            Directory.GetDirectories(directory);
            dir.GetDirectories();
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            lbShowInfo2.Items.Clear();
            DirectoryInfo dir = new DirectoryInfo(txtDirectory.Text);
            DirectoryInfo[] dirs=dir.GetDirectories(txtSelect.Text);
            foreach (var n in dirs)
            {
                lbShowInfo2.Items.Add(n);
            }
        }

        private void btnSecletF_Click(object sender, EventArgs e)
        {
            lbShowInfo2.Items.Clear();
            DirectoryInfo dir = new DirectoryInfo(txtDirectory.Text);
            FileInfo[] files = dir.GetFiles(txtSelect.Text);
            foreach(var n in files)
            {
                lbShowInfo2.Items.Add(n);
            }          
        }

        private void btnSelectAll_Click(object sender, EventArgs e)
        {
            lbShowInfo2.Items.Clear();
            DirectoryInfo dir = new DirectoryInfo(txtDirectory.Text);
            FileSystemInfo[] systeminfos = dir.GetFileSystemInfos(txtSelect.Text);
            foreach (var n in systeminfos)
            {
                lbShowInfo2.Items.Add(n);
            }
        } 
    }
}

file与fileinfo 的区别、Directory与DirectoryInfo

System.IO包含另一个类File,它的功能与FileInfo一样,不过不同的是,File类成员为静态。所以,使用File代替FileInfo就不必实例化一个新FileInfo对象。

      那么为什么有时还使用FileInfo呢?因为每次通过File类调用某个方法时,都要占用一定的cpu处理时间来进行安全检查,即使使用不同的File类的方法重复访问同一个文件时也是如此。而,FileInfo类只在创建FileInfo对象时执行一次安全检查。

public static class File System.IO 的成员,提供用于创建、复制、删除、移动和打开文件的静态方法,并协助创建 System.IO.FileStream 对象。 public sealed class FileInfo : System.IO.FileSystemInfo System.IO 的成员,提供创建、复制、删除、移动和打开文件的实例方法,并且帮助创建 System.IO.FileStream 对象。无法继承此类。 File提供的是静态方法,FileInfo提供的是实例方法 Directory、DirectoryInfo类似

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值