FileInfo与DirectoryInfo

2024-02-15 14:48
文章标签 fileinfo directoryinfo

本文主要是介绍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类似

这篇关于FileInfo与DirectoryInfo的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/711693

相关文章

Directory类(DirectoryInfo类)和Path类与File类的基本方法与操作以及实例

1 文件操作常用相关类 1)File //操作文件,静态类,对文件整体操作。拷贝、删除、剪切等。 2)Directory //操作目录(文件夹),静态类。 3)DirectoryInfo //文件夹的一个“类”,用来描述一个文件夹对象(获取指定目录下的所有目录时返回一个DirectoryInfo数组。) 1.FileInfo//文件类,用来描述一个文件对象。获取指定目录下的所有文件时,返回一个

c# 中的file和fileinfo,directory和directoryinfo以及filesysteminfo

在c#中提供了file和fileinfo类,这两个类的方法基本相同,以前只知道用,一直没弄明白究竟有什么区别,今天看书有点意外收获,与伙伴们共享:         file和fileinfo都提供对文件的操作         file类提供静态的方法,file类不能被继承,也不能产生实例,如果只需要使用一次或者次数很少,那么使用file类会比使用fileinfo效率高,但是file类每次

centos下为php安装fileinfo扩展随记

背景:项目接口上传图片失败,获取不到任何参数,网上搜索资料说是没有fileinfo扩展的原因。 1、在源码目录下找到fileinfo目录并进入 cd /xxx/xxx/php-7.2.7/ext/fileinfo/ 2、在该目录下执行phpize安装扩展。 /usr/local/php72/bin/phpize //php安装目录下的phpize位置 此处如果有报错的话,请根据错误提示安

Unable to guess the MIME type as no guessers are available (have you enabled the php_fileinfo extens

报错: [2022-01-10 08:41:12] local.ERROR: Unable to guess the MIME type as no guessers are available (have you enabled the php_fileinfo extension?). {"exception":"[object] (Symfony\\Component\\Mime\\Ex

c#directory 和directoryinfo的使用

当使用C#处理目录时,可以使用 System.IO 命名空间中的 Directory 和 DirectoryInfo 类来执行各种目录操作。以下是一些基本操作的示例: 创建目录 string directoryPath = @"C:\path\to\directory";Directory.CreateDirectory(directoryPath); 删除目录 string dire

Unable to guess the mime type as no guessers are available (Did you enable the php_fileinfo extensio

原因是没有开启php的php_fileinfo扩展,开启即可。 extension=php_fileinfo.dll 找到php.ini文件,搜索到php_fileinfo,去掉前面的分号,然后重启服务器apache。nginx下同理。 如果还有上述报错请查看 https://stackoverflow.com/questions/2306570

C# DirectoryInfo类的用法

在C#中,DirectoryInfo类是System.IO命名空间中的一个类,用于操作文件夹(目录)。通过DirectoryInfo类,我们可以方便地创建、删除、移动和枚举文件夹。本文将详细介绍DirectoryInfo类的常用方法和属性,并提供相应的代码示例。 目录 创建DirectoryInfo实例常用方法Create()Delete()MoveTo(string destD

6、FileInfo Exists

前言:System.IO下面的FileInfo类,继承至FileSystemInfo,是用来对系统中的文件进行创建、复制、删除等操作,Exists属性是对文件的判断,不是对文件夹的判断,这个很重要,不然可能会对需求做出错误的判断; 原文注释 //// 摘要:// Gets a value indicating whether a file exists.////