C#文件/文件夹操作
来源:互联网 发布:分销下单和传淘宝区别 编辑:程序博客网 时间:2024/04/28 10:08
当前程序所在的文件夹
System.IO.Directory.GetCurrentDirectory()
显示指定文件夹下的文件
if(this.textBox1.Text.Trim()=="")
return;
this.listBox1.Items.Clear();
string[] MyFiles=System.IO.Directory.GetFiles(this.textBox1.Text);
this.listBox1.Items.AddRange(MyFiles);
显示指定文件夹下的子文件夹
if(this.textBox1.Text.Trim()=="")
return;
this.listBox1.Items.Clear();
获取指定文件夹下的所有子文件夹
string[] MyFolders=System.IO.Directory.GetDirectories(this.textBox1.Text);
this.listBox1.Items.AddRange(MyFolders);
同时显示指定文件夹下的子文件夹和文件
if(this.textBox1.Text.Trim()=="")
return;
this.listBox1.Items.Clear();
获取指定文件夹下的所有文件和子文件夹
string[] MyFoldersFiles=System.IO.Directory.GetFileSystemEntries(this.textBox1.Text);
this.listBox1.Items.AddRange(MyFoldersFiles);
文件创建时间
this.dateTimePicker1.Text=File.GetCreationTime(this.textBox1.Text).ToLongDateString();
最近修改时间
this.dateTimePicker2.Text=File.GetLastWriteTime(this.textBox1.Text).ToLongDateString();
最近访问时间
this.dateTimePicker3.Text=File.GetLastAccessTime(this.textBox1.Text).ToLongDateString();
FileAttributes MyAttributes=File.GetAttributes(this.textBox1.Text);
string MyFileType=MyAttributes.ToString();
if(MyFileType.LastIndexOf("ReadOnly")!=-1) //是否只读文件
{
this.checkBox1.Checked=true;
}
if(MyFileType.LastIndexOf("System")!=-1) //是否系统文件
{
this.checkBox2.Checked=true;
}
if(MyFileType.LastIndexOf("Hidden")!=-1) //是否隐藏文件
{
this.checkBox3.Checked=true;
}
if(MyFileType.LastIndexOf("Archive")!=-1) //是否归档文件
{
this.checkBox4.Checked=true;
}
if(MyFileType.LastIndexOf("Temporary")!=-1) //是否临时文件
{ this.checkBox5.Checked=true;
}
设置文件属性
if(this.textBox1.Text.Length<2)
return;
File.SetAttributes(this.textBox1.Text, FileAttributes.Normal);
if(this.checkBox1.Checked==true)
{
File.SetAttributes(this.textBox1.Text, FileAttributes.ReadOnly);
}
FileAttributes MyAttributes=File.GetAttributes(this.textBox1.Text);
if(this.checkBox2.Checked==true)
{
File.SetAttributes(this.textBox1.Text,MyAttributes|FileAttributes.System);
}
MyAttributes=File.GetAttributes(this.textBox1.Text);
if(this.checkBox3.Checked==true)
{
File.SetAttributes(this.textBox1.Text,MyAttributes|FileAttributes.Hidden);
}
MyAttributes=File.GetAttributes(this.textBox1.Text);
if(this.checkBox4.Checked==true)
{
File.SetAttributes(this.textBox1.Text,MyAttributes|FileAttributes.Archive);
}
MyAttributes=File.GetAttributes(this.textBox1.Text);
if(this.checkBox5.Checked==true)
{
File.SetAttributes(this.textBox1.Text,MyAttributes|FileAttributes.Temporary);
}
File.SetCreationTime(this.textBox1.Text,this.dateTimePicker1.Value);
File.SetLastWriteTime(this.textBox1.Text,this.dateTimePicker2.Value);
File.SetLastAccessTime(this.textBox1.Text,this.dateTimePicker3.Value);
MessageBox.Show("设置文件属性操作成功!","信息提示",*******);
获取文件夹属性
if(this.textBox1.Text.Length<2)
return;
获取文件夹创建时间
this.dateTimePicker1.Text=Directory.GetCreationTime(this.textBox1.Text).ToLongDateString();
获取文件夹最近被修改时间
this.dateTimePicker2.Text=Directory.GetLastWriteTime(this.textBox1.Text).ToLongDateString();
获取文件夹最近被访问时间
this.dateTimePicker3.Text=Directory.GetLastAccessTime(this.textBox1.Text).ToLongDateString();
取得文件夹属性
FileAttributes MyAttributes=File.GetAttributes(this.textBox1.Text);
string MyFileType=MyAttributes.ToString();
if(MyFileType.LastIndexOf("Hidden")!=-1)
{
判断文件夹隐藏属性
this.checkBox3.Checked=true;
}
if(MyFileType.LastIndexOf("Archive")!=-1)
{
判断文件夹归档属性
this.checkBox4.Checked=true;
}
设置文件夹属性
if(this.textBox1.Text.Length<2)
return;
设置文件夹属性为正常
File.SetAttributes(this.textBox1.Text, FileAttributes.Normal);
FileAttributes MyAttributes=File.GetAttributes(this.textBox1.Text);
if(this.checkBox3.Checked==true)
{
设置文件夹隐藏属性
File.SetAttributes(this.textBox1.Text,MyAttributes|FileAttributes.Hidden);
}
MyAttributes=File.GetAttributes(this.textBox1.Text);
if(this.checkBox4.Checked==true)
{
设置文件夹归档属性
File.SetAttributes(this.textBox1.Text,MyAttributes|FileAttributes.Archive);
设置文件夹创建时间
Directory.SetCreationTime(this.textBox1.Text,this.dateTimePicker1.Value);
设置文件夹最近被修改时间
Directory.SetLastWriteTime(this.textBox1.Text,this.dateTimePicker2.Value);
设置文件夹最近被访问时间
Directory.SetLastAccessTime(this.textBox1.Text,this.dateTimePicker3.Value);
MessageBox.Show("设置文件夹属性操作成功!","信息提示",*******);
判断文件是否已经存在
string MyFileName=this.textBox1.Text;
if(MyFileName.Length<1)
return;
string ShortName=MyFileName.Substring(MyFileName.LastIndexOf("//")+1);
if(File. Exists(MyFileName))
{
MessageBox.Show("文件:"+ShortName+"已经存在!","信息提示",*****);}
else
{
MessageBox.Show("文件:"+ShortName+"不存在!","信息提示",*****);
}
判断文件夹是否已经存在
string MyFolderName=this.textBox2.Text;
if(MyFolderName.Length<1)
return;
string FolderName=MyFolderName.Substring(MyFolderName.LastIndexOf("//")+1);
if(Directory.Exists(MyFolderName))
{
MessageBox.Show("文件夹:"+FolderName+"已经存在!","信息提示",*****);
}
else
{
MessageBox.Show("文件夹:"+FolderName+"不存在!","信息提示",*****);
}
删除文件夹
if(this.textBox1.Text.Trim()=="")
return;
DirectoryInfo MyDir=new DirectoryInfo(this.textBox1.Text);
if(MessageBox.Show("是否删除文件夹:"+this.textBox1.Text+"及其所有内容?","提示",MessageBoxButtons.YesNo,MessageBoxIcon.Question) == DialogResult.Yes)
{
MyDir.Delete(true);
this.textBox1.Text="";
}
创建多层文件夹
if(this.textBox2.Text.Trim()=="")
return;
if(Directory.Exists(this.textBox2.Text))
{
MessageBox.Show("该文件夹已经存在!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
if(MessageBox.Show("是否创建多层文件夹:"+this.textBox2.Text,"提示",MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes)
{
Directory.CreateDirectory(this.textBox2.Text);
this.textBox2.Text="";
}
- C#文件/文件夹操作
- C#文件、文件夹操作
- C# 操作文件夹及文件
- C# 操作文件夹及文件
- C# 操作文件夹及文件
- C# 操作文件夹及文件
- C# 文件,文件夹的操作
- C#操作文件以及文件夹
- C#文件和文件夹操作
- C#常用 文件、文件夹 操作
- C#文件与文件夹操作
- C#操作文件及文件夹
- C# 文件操作(文件夹 及 文件)
- C# 文件操作(文件夹 及 文件) .
- C#读取文件夹中的文件操作浅析
- C# FTP 文件 文件夹操作 上传 下载
- C# FTP 文件 文件夹操作 上传 下载
- C# 文件和文件夹的操作
- 多普达818上网设置详细图解
- 我这家伙很懒!!
- 何必言精通——十年杂感 兼谈其它(转载)
- session iframe 丢失
- 手机校时专家(Time Expert V1.4)SNTP校时出错的原因
- C#文件/文件夹操作
- 在线客服QQ代码
- oracle rownum 原理及分页查询实现
- jbpm流程回退详解
- java汉字转拼音
- 远程桌面控制Linux/unix
- shp2sdo的下载及使用说明
- 在ORACLE中创建自增字段
- 当你添加了一个索引后,你的查询语句出错了...