C#文件和目录的操作

 

根据文件名获取文件

 1  /// <summary>
 2         /// 根据文件名获取文件
 3         /// </summary>
 4         /// <param name="directory"></param>
 5         /// <param name="fileName"></param>
 6         /// <param name="searchOption"></param>
 7         /// <returns></returns>
 8         private FileInfo[] GetFile(string directory, string fileName, SearchOption searchOption)
 9         {
10             DirectoryInfo directoryInfo = new DirectoryInfo(directory);
11             if (!directoryInfo.Exists)
12                 return null;
13 
14             return directoryInfo.GetFiles(fileName, searchOption);
15         }
View Code

删除过期目录

 1 /// <summary>
 2         /// 删除过期目录
 3         /// </summary>
 4         /// <param name="directory">根目录</param>
 5         /// <param name="day">期限</param>
 6         /// <returns></returns>
 7         private int DelectOverdueDirectorys(string directory, int day)
 8         {
 9             DirectoryInfo directoryInfoRoot = new DirectoryInfo(directory);
10             if (!directoryInfoRoot.Exists)
11                 return 1;
12 
13             DirectoryInfo[] directoryInfos = directoryInfoRoot.GetDirectories();
14             foreach (var directoryInfo in directoryInfos)
15             {
16                 if (DateTime.Now.Subtract(directoryInfo.CreationTime).TotalDays > day)
17                     directoryInfo.Delete(true);
18             }
19 
20             return 0;
21         }
View Code

删除过期文件

 1 /// <summary>
 2         /// 删除过期文件
 3         /// </summary>
 4         /// <param name="directory">根目录</param>
 5         /// <param name="day">期限</param>
 6         /// <returns></returns>
 7         private int DelectOverdueFiles(string directory, int day)
 8         {
 9             DirectoryInfo directoryInfoRoot = new DirectoryInfo(directory);
10             if (!directoryInfoRoot.Exists)
11                 return 1;
12 
13             FileInfo[] fileInfos = directoryInfoRoot.GetFiles();
14             foreach (var fileInfo in fileInfos)
15             {
16                 if (DateTime.Now.Subtract(fileInfo.CreationTime).TotalDays > day)
17                     fileInfo.Delete();
18             }
19 
20             return 0;
21         }
View Code

设置目录隐藏

 1 /// <summary>
 2         /// 设置目录隐藏
 3         /// </summary>
 4         /// <param name="directory"></param>
 5         /// <returns></returns>
 6         private int SetDirectoryHide(string directory)
 7         {
 8             DirectoryInfo directoryInfo = new DirectoryInfo(directory);
 9             if (!directoryInfo.Exists)
10                 return 1;
11 
12             directoryInfo.Attributes = FileAttributes.Hidden;
13             return 0;
14         }
View Code

目录加解锁

 1 string FileExtend = ".{2559a1f2-21d7-11d4-bdaf-00c04f60b9f0}";
 2 
 3         /// <summary>
 4         /// 目录加锁
 5         /// </summary>
 6         /// <param name="directory"></param>
 7         /// <returns></returns>
 8         private int DirectoryEncrypt(string directory)
 9         {
10             DirectoryInfo directoryInfo = new DirectoryInfo(directory);
11             if (!directoryInfo.Exists)
12                 return 1;
13 
14             directoryInfo.MoveTo(directory + FileExtend);
15             return 0;
16         }
17 
18         /// <summary>
19         /// 目录解锁
20         /// </summary>
21         /// <param name="directory"></param>
22         /// <returns></returns>
23         private int DirectoryDecrypt(string directory)
24         {
25             DirectoryInfo directoryInfo = new DirectoryInfo(directory + FileExtend);
26             if (!directoryInfo.Exists)
27                 return 1;
28 
29             directoryInfo.MoveTo(directory);
30             return 0;
31         }
View Code

 

posted @ 2019-04-17 17:53  翻白眼的哈士奇  阅读(119)  评论(0编辑  收藏  举报