.Net文件*夹*操作

一、文件夹操作

Directory类,DirectoryInfo类.使用using System.IO命名空间

(一)创建文件夹

方法一:

1         private string path = @"F:\Text\ceshi";
2         private void Create_Click(object sender, EventArgs e)
3         {
4             Directory.CreateDirectory(path);
5         }    

方法二:

1         private string path = @"F:\Text\ceshi";
2         private void Create_Click(object sender, EventArgs e)
3         {
4 5 DirectoryInfo CreateDirectory = new DirectoryInfo(path); 6 CreateDirectory.Create(); 7 }

 

(二)删除文件夹

方法一:

1         private string path = @"F:\Text\ceshi";
2         private void Delete_Click(object sender, EventArgs e)
3         {
4             Directory.Delete(path);
5         }

 方法二:

1         private string path = @"F:\Text\ceshi";
2         private void Delete_Click(object sender, EventArgs e)
3         {           
4             DirectoryInfo DeleteDircetory = new DirectoryInfo(path);
5             DeleteDircetory.Delete();
6         }

 

(三)判断文件夹是否存在

方法一:

 1         private string path = @"F:\Text\ceshi";
 2         private void Exist_Click(object sender, EventArgs e)
 3         {
 4             bool ifExist = Directory.Exists(path);           
 5             if (ifExist)
 6             {
 7                 MessageBox.Show("已存在");
 8             }
 9             else
10             {
11                 MessageBox.Show("不存在");
12             }
13         }

 

方法二:

 1         private string path = @"F:\Text\ceshi";
 2         private void Exist_Click(object sender, EventArgs e)
 3         {            
 4             DirectoryInfo ExistDirectory = new DirectoryInfo(path);
 5             bool ifExist = ExistDirectory.Exists;
 6             if (ifExist)
 7             {
 8                 MessageBox.Show("已存在");
 9             }
10             else
11             {
12                 MessageBox.Show("不存在");
13             }
14         }

 

(四)获取子文件夹

注:获取子文件只能是获取--该路径下的文件夹,其他非文件夹格式获取不到

方法一:

1         private void GetDirectory_Click(object sender, EventArgs e)
2         {
3             string[] Dire =  Directory.GetDirectories(@"F:\Text");//返回的字符串是全路径加文件夹名称(如:"F:\Text\ceshi")
4             listBox_GetDiretory.Items.Clear();
5             listBox_GetDiretory.Items.AddRange(Dire);
6         }

 

方法二:

1         private void GetDirectory_Click(object sender, EventArgs e)
2         {           
3             DirectoryInfo GetDirectory = new DirectoryInfo(@"F:\Text");//只返回文件夹的名字
4             DirectoryInfo[] Dire = GetDirectory.GetDirectories();
5             listBox_GetDiretory.Items.Clear();
6             listBox_GetDiretory.Items.AddRange(Dire);
7         }

 

(五)获取子文件

注:获取子文件只获取--除了文件夹以外其他的文件

方法一:

1         private void GetFile_Click(object sender, EventArgs e)
2         {
3             string[] file = Directory.GetFiles(@"F:\Text");//返回的字符串是全路径加文件名称(如:"F:\Text\ceshi")
4             listBox_GetFile.Items.AddRange(file);
5         }

 

方法二:

1         private void GetFile_Click(object sender, EventArgs e)
2         {            
3             DirectoryInfo GetFile = new DirectoryInfo(@"F:\Text");//只返回文件名称
4             FileInfo[] file = GetFile.GetFiles();
5             listBox_GetFile.Items.AddRange(file);
6         }

 

(六)获取文件夹的相关属性

注:日期时间后面Utc代表格列尼日时间

方法一:

1         private string path = @"F:\Text\ceshi";
2         private void GetInfo_Click(object sender, EventArgs e)
3         {
4             DateTime CreateTime = Directory.GetCreationTime(path); //获取创建时间
5             DateTime AccessTime = Directory.GetLastAccessTime(path);//获取最后访问时间
6             DateTime WriteTime = Directory.GetLastWriteTime(path); //获取最后写入的时间
7             DirectoryInfo.Text = CreateTime.ToString() + "\n" + AccessTime.ToString() + "\n" + WriteTime.ToString();
8         }

 

方法二:

1         private string path = @"F:\Text\ceshi";
2         private void GetInfo_Click(object sender, EventArgs e)
3         {       
4             DirectoryInfo DireInfo = new System.IO.DirectoryInfo(path);
5             Info.Text = DireInfo.CreationTime.ToString() + "\n" + DireInfo.LastAccessTime.ToString() + "\n" + DireInfo.LastWriteTime.ToString();
6         }

 

(七)修改文件夹的相关属性

方法一:

1         private string path = @"F:\Text\ceshi";
2         private void SetInfo_Click(object sender, EventArgs e)
3         {
4             DateTime T = DateTime.Now;
5             Directory.SetCreationTime(path, T);
6             Directory.SetLastAccessTime(path, T);
7             Directory.SetLastWriteTime(path, T);
8         }

 

方法二:

(.............)

(八)移动文件夹(或改名)

注:

Move()或MoveTo():1.路径不同名字相同(相当于--剪切),

           2.路径不同名字不同(相当于--剪切+重命名),

           3.路径相同名字不同(相当于--重命名).

 且移动是同时将文件夹内的所有对象,移动.

方法一:

1         private string path = @"F:\Text\ceshi";
2         private void MoveDirectory_Click(object sender, EventArgs e)
3         {
4             Directory.Move(path, @"F:\Test");
5         }

方法二:

1         private string path = @"F:\Text\ceshi";
2         private void MoveDirectory_Click(object sender, EventArgs e)
3         {            
4             DirectoryInfo MoveDirectory = new DirectoryInfo(path);
5             MoveDirectory.MoveTo(@"F:\ceshi2");
6         }

 

posted @ 2015-07-24 09:10  北极熊爱吃鱼  阅读(179)  评论(0编辑  收藏  举报