C#文件夹操作
一、文件夹操作
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 }
方法二:
温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/67942.html
- 上一篇:git svn cygwin
- 下一篇:C#WebFrom六大验证及使用方法