• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#文件和目录的操作

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 

根据文件名获取文件

 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

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#基于.net开发以太坊智能合约发布时间:2022-07-14
下一篇:
C#文件和文件夹操作发布时间:2022-07-14
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap