首页 > 代码库 > System.IO.Directory类

System.IO.Directory类

1、参考的博客:System.IO.Directory类和System.DirectoryInfo类(http://blog.sina.com.cn/s/blog_614f473101017du4.html);


 

2、Directory 类:https://msdn.microsoft.com/zh-cn/library/system.io.directory.aspx


 

3、自己实践:

技术分享

 1 using System;
 2 using System.Windows.Forms;
 3 using System.IO;
 4 
 5 namespace Directory_use
 6 {
 7     public partial class Form1 : Form
 8     {
 9         public Form1()
10         {
11             InitializeComponent();
12         }
13 
14         private void button1_Click(object sender, EventArgs e)        //生成目录 
15         {
16             MakeDirectory();
17         }
18 
19         private void button2_Click(object sender, EventArgs e)       //退出应用程序
20         {
21             Application.Exit();
22         }
23 
24         private void button3_Click(object sender, EventArgs e)
25         {
26             if (File.Exists(@"c:\temp00\NewDirectory"))    //判断目录是否存在  
27             {
28                 
29                 DeleteDirectory(); //删除目录   
30             }
31             else
32             {
33                 MakeDirectory(); //生成目录  
34                 //SetDirectory(); //设置目录属性  
35             }
36         }
37         private void button4_Click(object sender, EventArgs e)       //移动目录
38         {
39             if (File.Exists(@"c:\temp00\NewDirectory"))    //判断目录是否存在  
40             {
41                 File.Move(@"c:\temp00\NewDirectoty", @"c:\temp01\BackUp");
42             }
43             else
44             {
45                 MakeDirectory();     //生成目录  
46                 //SetDirectory();   //设置目录属性
47             }
48            
49         }
50 
51         private void button5_Click(object sender, EventArgs e)
52         {
53             dataGridView1.DataSource = GetDirectory();
54         }
55 
56         private void GetFile()      //获取文件 
57         {
58             string[] Files;
59             Files = Directory.GetFiles(@"c:\temp00");
60         }
61 
62         private string[] GetDirectory()  //获取子目录
63         {
64             string[] Directorys;
65             Directorys = Directory.GetDirectories(@"c:\temp00");
66             return Directorys;
67         }
68 
69         private void SetDirectory()     //设置目录属性
70         {
71             DirectoryInfo NewDirInfo = new DirectoryInfo(@"c:\temp00\NewDirectoty");
72             NewDirInfo.Attributes = FileAttributes.ReadOnly | FileAttributes.Hidden;
73         }
74 
75         private void MakeDirectory()   //生成目录
76         {
77             Directory.CreateDirectory(@"c:\temp00\NewDirectoty");
78         }
79 
80         private void DeleteDirectory() //删除目录 
81         {
82             Directory.Delete(@"c:\temp00\BackUp", true);
83         }
84 
85        
86     }
87 }

【欢迎转载】

 转载请表明出处: 乐学习

 

System.IO.Directory类