WinForms 获取文件夹的基本信息
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 using System.IO; 10 11 namespace WindowsFormsApplication4 12 { 13 public partial class Form1 : Form 14 { 15 public Form1() 16 { 17 InitializeComponent(); 18 } 19 20 private static string directory_path = "D:\\qs250"; 21 private static string directory_otherpath = "D:\\qqqq"; 22 /// <summary> 23 /// 创建目录 24 /// </summary> 25 /// <param name="sender"></param> 26 /// <param name="e"></param> 27 private void button1_Click(object sender, EventArgs e) 28 { 29 try 30 { 31 Directory.CreateDirectory(directory_path); 32 button1.Enabled = false; //点击按钮后,此按钮即为灰色; 33 button2.Enabled = true; 34 button3.Enabled = true; 35 button4.Enabled = true; 36 button5.Enabled = true; 37 MessageBox.Show("文件夹创建成功!","警报!"); 38 } 39 catch (Exception em) 40 { 41 42 MessageBox.Show("磁盘操作错误,原因:" + Convert.ToString(em),"警报"); 43 } 44 } 45 /// <summary> 46 /// 删除目录 47 /// </summary> 48 /// <param name="sender"></param> 49 /// <param name="e"></param> 50 private void button2_Click(object sender, EventArgs e) 51 { 52 try 53 { 54 Directory.Delete(directory_path); 55 button1.Enabled = true; 56 button2.Enabled = false; 57 button3.Enabled = false; 58 button4.Enabled = false; 59 button5.Enabled = false; 60 MessageBox.Show("文件夹删除成功!", "警报!"); 61 } 62 catch (Exception em) 63 { 64 65 MessageBox.Show("磁盘操作错误,原因:" + Convert.ToString(em), "警报"); 66 } 67 } 68 /// <summary> 69 /// 移动目录 70 /// </summary> 71 /// <param name="sender"></param> 72 /// <param name="e"></param> 73 private void button3_Click(object sender, EventArgs e) 74 { 75 try 76 { 77 Directory.Move(directory_path,directory_otherpath); 78 MessageBox.Show("文件夹移动成功!", "警报!"); 79 } 80 catch (Exception em) 81 { 82 83 MessageBox.Show("磁盘操作错误,原因:" + Convert.ToString(em), "警报"); 84 } 85 } 86 /// <summary> 87 /// 目录创建时间 88 /// </summary> 89 /// <param name="sender"></param> 90 /// <param name="e"></param> 91 private void button4_Click(object sender, EventArgs e) 92 { 93 try 94 { 95 96 MessageBox.Show(string.Format("{0:G}",Directory.GetCreationTime(directory_path)), "提示!"); 97 } 98 catch (Exception em) 99 { 100 101 MessageBox.Show("磁盘操作错误,原因:" + Convert.ToString(em), "警报"); 102 } 103 } 104 /// <summary> 105 /// 返回指定目录 106 /// </summary> 107 /// <param name="sender"></param> 108 /// <param name="e"></param> 109 private void button5_Click(object sender, EventArgs e) 110 { 111 try 112 { 113 string[] fileEntries = Directory.GetFiles(directory_path); 114 if (fileEntries.Length != 0) 115 { 116 foreach (var s in fileEntries) 117 { 118 if (File.Exists(s)) 119 { 120 MessageBox.Show("内有文件信息:" + s, "提示!"); 121 } 122 else 123 { 124 MessageBox.Show("空文件夹", "提示!"); 125 } 126 } 127 } 128 } 129 catch(Exception em) 130 { 131 MessageBox.Show("磁盘操作错误,原因:" + Convert.ToString(em), "警报"); 132 } 133 } 134 } 135 }
必须要亲自动手写代码才能有切身的理解与体会!