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类似