C#Winform文件操作总结

来源:互联网 发布:淘宝买家采集 编辑:程序博客网 时间:2024/04/28 14:06

1.当前程序所在的文件夹 
System.IO.Directory.GetCurrentDirectory()


2.显示指定文件夹下的文件 
if(this.textBox1.Text.Trim()=="")
    return;
this.listBox1.Items.Clear();
string[] MyFiles=System.IO.Directory.GetFiles(this.textBox1.Text);
this.listBox1.Items.AddRange(MyFiles);


3.显示指定文件夹下的子文件夹 
if(this.textBox1.Text.Trim()=="")
    return;
this.listBox1.Items.Clear();


4.获取指定文件夹下的所有子文件夹 
string[] MyFolders=System.IO.Directory.GetDirectories(this.textBox1.Text);
this.listBox1.Items.AddRange(MyFolders);


5.同时显示指定文件夹下的子文件夹和文件 
if(this.textBox1.Text.Trim()=="")
    return;
this.listBox1.Items.Clear();


6.获取指定文件夹下的所有文件和子文件夹 
string[] MyFoldersFiles=System.IO.Directory.GetFileSystemEntries(this.textBox1.Text);
this.listBox1.Items.AddRange(MyFoldersFiles);


7.文件创建时间
this.dateTimePicker1.Text=File.GetCreationTime(this.textBox1.Text).ToLongDateString();


8.最近修改时间
this.dateTimePicker2.Text=File.GetLastWriteTime(this.textBox1.Text).ToLongDateString();


9.最近访问时间
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;
}


10.设置文件属性
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("设置文件属性操作成功!","信息提示",*******);


11.获取文件夹属性
if(this.textBox1.Text.Length<2)
    return;


12.获取文件夹创建时间
this.dateTimePicker1.Text=Directory.GetCreationTime(this.textBox1.Text).ToLongDateString();


13.获取文件夹最近被修改时间
this.dateTimePicker2.Text=Directory.GetLastWriteTime(this.textBox1.Text).ToLongDateString();


14.获取文件夹最近被访问时间
this.dateTimePicker3.Text=Directory.GetLastAccessTime(this.textBox1.Text).ToLongDateString();


15.取得文件夹属性
FileAttributes MyAttributes=File.GetAttributes(this.textBox1.Text);
string MyFileType=MyAttributes.ToString();
if(MyFileType.LastIndexOf("Hidden")!=-1)
{


16.判断文件夹隐藏属性
    this.checkBox3.Checked=true;
}
if(MyFileType.LastIndexOf("Archive")!=-1)
{


17.判断文件夹归档属性
    this.checkBox4.Checked=true;
}


18.设置文件夹属性
if(this.textBox1.Text.Length<2)
    return;


19.设置文件夹属性为正常
File.SetAttributes(this.textBox1.Text, FileAttributes.Normal);
FileAttributes MyAttributes=File.GetAttributes(this.textBox1.Text);
if(this.checkBox3.Checked==true)
{


20.设置文件夹隐藏属性
    File.SetAttributes(this.textBox1.Text,MyAttributes|FileAttributes.Hidden);
}
MyAttributes=File.GetAttributes(this.textBox1.Text);
if(this.checkBox4.Checked==true)
{


21.设置文件夹归档属性
    File.SetAttributes(this.textBox1.Text,MyAttributes|FileAttributes.Archive);


22.设置文件夹创建时间
Directory.SetCreationTime(this.textBox1.Text,this.dateTimePicker1.Value);


23.设置文件夹最近被修改时间
Directory.SetLastWriteTime(this.textBox1.Text,this.dateTimePicker2.Value);


24.设置文件夹最近被访问时间
Directory.SetLastAccessTime(this.textBox1.Text,this.dateTimePicker3.Value);
MessageBox.Show("设置文件夹属性操作成功!","信息提示",*******);


25.判断文件是否已经存在
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+"不存在!","信息提示",*****);
}


26.判断文件夹是否已经存在
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+"不存在!","信息提示",*****);



27.删除文件夹
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="";
}


28.创建多层文件夹
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="";
}



PS:

//C#追加文件 
StreamWriter sw = File.AppendText(Server.MapPath(".")+"\\myText.txt");
sw.WriteLine(
"追逐理想");
sw.WriteLine(
"kzlll");
sw.WriteLine(
".NET笔记");
sw.Flush();
sw.Close();

//C#拷贝文件
string OrignFile,NewFile;
OrignFile
= Server.MapPath(".")+"\\myText.txt";
NewFile
= Server.MapPath(".")+"\\myTextCopy.txt";
File.Copy(OrignFile,NewFile,
true);

//C#删除文件
string delFile = Server.MapPath(".")+"\\myTextCopy.txt";
File.Delete(delFile);

//C#移动文件
string OrignFile,NewFile;
OrignFile
= Server.MapPath(".")+"\\myText.txt";
NewFile
= Server.MapPath(".")+"\\myTextCopy.txt";
File.Move(OrignFile,NewFile);

//C#创建目录
// 创建目录c:\sixAge
DirectoryInfo d=Directory.CreateDirectory("c:\\sixAge");
// d1指向c:\sixAge\sixAge1
DirectoryInfo d1=d.CreateSubdirectory("sixAge1");
// d2指向c:\sixAge\sixAge1\sixAge1_1
DirectoryInfo d2=d1.CreateSubdirectory("sixAge1_1");
// 将当前目录设为c:\sixAge
Directory.SetCurrentDirectory("c:\\sixAge");
// 创建目录c:\sixAge\sixAge2
Directory.CreateDirectory("sixAge2");
// 创建目录c:\sixAge\sixAge2\sixAge2_1
Directory.CreateDirectory("sixAge2\\sixAge2_1");

//递归删除文件夹及文件
<%@ Page Language=C#%>
<%@ Import namespace="System.IO"%>
<Script runat=server>
public void DeleteFolder(string dir)
{
if (Directory.Exists(dir)) //如果存在这个文件夹删除之
{
foreach(string d in Directory.GetFileSystemEntries(dir))
{
if(File.Exists(d))
File.Delete(d);
//直接删除其中的文件
else
DeleteFolder(d);
//递归删除子文件夹
}
Directory.Delete(dir);
//删除已空文件夹
Response.Write(dir+" 文件夹删除成功");
}
else
Response.Write(dir
+" 该文件夹不存在"); //如果文件夹不存在则提示
}

protected void Page_Load (Object sender ,EventArgs e)
{
string Dir="D:\\gbook\\11";
DeleteFolder(Dir);
//调用函数删除文件夹
}

0 0
原创粉丝点击