读书笔记(FileInfo DirectoryInfo)

  .net内建类型中提供了FileInfo和DirectoryInfo两个类型,用于操作文件和文件夹,有别于File 和Directory, FileInfo 和DirectoryInfo主要用于操作文件和文件夹系统中的结构,完成诸如创建,复制,读取信息,移动,判断是否存在,删除等功能

 1        static void ReadFileInfo(FileInfo fi) 
 2         {
 3             Console.WriteLine("fileName: "+fi.Name);
 4             Console.WriteLine("fileDirectoryName: "+fi.DirectoryName);
 5             Console.WriteLine("fileLength: "+fi.Length);
 6             Console.WriteLine("fileCreationTime "+fi.CreationTime);
 7             Console.WriteLine("fileLastWriteTime "+fi.LastWriteTime);
 8             Console.WriteLine("fileLastAccessTime "+fi.LastAccessTime);
 9             Console.WriteLine("fileFullName "+fi.FullName);
10             Console.WriteLine("fileExtension "+fi.Extension);
11         }
12 
13         static void ReadDirectoryInfo(DirectoryInfo di) 
14         {
15             Console.WriteLine("DirectoryName: " +di.Name);
16             Console.WriteLine("DirectoryParentName: "+di.Parent.Name);
17             Console.WriteLine("DirectoryLastWriteTime: "+di.LastWriteTime);
18             Console.WriteLine("DirectoryLastAccessTime: "+di.LastAccessTime);
19             Console.WriteLine("DirectoryFullName: "+di.FullName);
20             FileInfo[] fis = di.GetFiles();
21             foreach (FileInfo fi in fis)
22                 ReadFileInfo(fi);
23             DirectoryInfo[] dis = di.GetDirectories();
24             foreach (DirectoryInfo ndi in dis)
25                 ReadDirectoryInfo(ndi);
26         }
27 
28         static void DirectoryTest() 
29         {
30             DirectoryInfo di = null;
31             try
32             {
33                 di = new DirectoryInfo("C:\\Test");
34                 di.Create();
35                 di.CreateSubdirectory("subDi");
36                 FileInfo info = new FileInfo("C:\\Test\\subDi\\test.txt");
37                 using (FileStream fs = info.Create()) { }
38                 ReadDirectoryInfo(di);
39                 di.MoveTo("C:\\Test2");
40             }
41             finally 
42             {
43                 if (di != null) 
44                 {
45                     //删除文件夹
46                     //一起删除文件和子文件夹
47                     di.Delete(true);
48                 }
49             }
50         }
51 
52         static void FileTest() 
53         {
54             FileInfo info = null;
55             FileInfo newinfo = null;
56             try
57             {
58                 info = new FileInfo("C:\\test1.txt");
59                 using (FileStream fs = info.Create()) { }
60                 ReadFileInfo(info);
61                 info.MoveTo("C:\\test2.txt");
62                 newinfo = info.CopyTo("C:\\test3.txt");
63                 ReadFileInfo(newinfo);
64             }
65             finally 
66             {
67                 if (info != null)
68                     info.Delete();
69                 if (newinfo != null)
70                     newinfo.Delete();
71             }
72         }

 

posted @ 2014-07-31 12:11  sophiadv  阅读(158)  评论(0编辑  收藏  举报