数据库A表添加一条记录
,**系统中B目录下就会多出5n个文件
。随着系统运行3年多
,B目录中的文件数已高达2M多,而这些文件恰恰又是用户高度频繁访问的
。于是问题就来了,一方面是用户访问文件速度变慢了;另一方面是文件太多,很难维护。
怎么办呢?思许良久,发现A表中有个录入时间字段是不会变更的。如果截取录入时间的年份+月份组成,用来创建B目录下的子目录名,把当年当月新增的文件统一归档于该子目录下,不就可以吗?新增的文件好处理,可对于旧文件归档需要费点周折,因为文件得迁移到新的子目录里。
上面例子参考了MSDN 关于File Class 和 Directory Class 的使用方法。
执行效果图如下:

Tips:
目录名(年份+月份) 如:201101
c# => DateTime.Now.ToString("yyyyMM")
SQL => convert(varchar(6),getdate(),112)
当然仅仅文件迁移是不够的,还有很多工作要做,比如修改程序;更新数据库表记录等等。我知道,这次“手术”不符合开放-关闭原则。
下面是关于文箭╧y"http://www.hacker.com.cn/" target="_blank" class="keylink">seo0sa1xNb30qq0+sLro7o8YnIgLz4NCiZuYnNwOzwvcD4NCjxvbCBjbGFzcz0="dp-c">
- static void Main(string[] args)
- {
- string paperPath = ConfigurationManager.AppSettings["PaperBuildPath"];
- Console.WriteLine(string.Format("试卷目录:{0}", paperPath));
- Console.WriteLine();
- Console.WriteLine("目录是否正确? 正确请按任意键......");
- Console.WriteLine();
- Console.ReadKey();
- string[] files = Directory.GetFiles(paperPath);
- int num = 0;
- PublicExam[] list = Gateway.Default.FindArray<PublicExam>();
- foreach (PublicExam publicExam in list)
- {
- foreach (string file in files)
- {
-
- string fileName = file.Split('\\').Last();
- if (fileName.StartsWith(publicExam.FGuid.ToString(), StringComparison.CurrentCultureIgnoreCase))
- {
-
- string destFilePath = paperPath + publicExam.FInputTime.ToString("yyyyMM");
- if (Directory.Exists(destFilePath) == false)
- Directory.CreateDirectory(destFilePath);
-
- string destFileName = destFilePath + "\\" + fileName;
- if (File.Exists(destFileName))
- File.Delete(destFileName);
- Console.WriteLine(string.Format("正在迁移文件:{0}", fileName));
-
- File.Move(file, destFileName);
- num++;
- }
- }
- }
- Console.WriteLine();
- Console.WriteLine(string.Format("共迁移{0}个文件,请按任意键退出......", num));
- Console.ReadKey();
- }
希望这篇
的文章能够对您有所帮助,如果您觉得这篇网站维护教程有用的话,别忘了推荐给您的朋友哦!如果您有好的经验方法,不妨拿出来和大家一起分享:假如每个人都拿出一个经验,那么我们都将额外的获取一堆他人的经验。
请记住本站永久域名:(黑客防线网安服务器维护方案维护基地)Www.Rongsen.Com.Cn