C#文件操作
我想写一个程序,用到app.setting,使得文件夹里面没有数据的自动删除,最后一次修改与现在时间差10天的自动删除,请赐教谢谢~~~
就是相当一个自动删除的工具 ,文件夹里面没东西就删掉,最后一次访问时间超过多少就删掉,现在部分问题解决了,如下问题是:
1、怎么配置app。setting,没有用到啊感觉,但是上面要求用到这个
2、我要是有一个文件夹的子文件夹有内容怎么判断,要是文件在最深一层,那查起来太麻烦了吧,谢谢
代码:
using System;
using System.Collections.Generic;
using System.Text;
using
namespace timespans
{
class Program
{
static void Main(string[] args)
{
DirectoryInfo di = new DirectoryInfo("F:\\win03\\");
// Get a reference to each file in that directory.
FileInfo[] fiArr = di.GetFiles();
DirectoryInfo[] diArr =di.GetDirectories();
// Display the names of the files.
foreach (FileInfo fri in fiArr)
{
DateTime DateTime2 = DateTime.Now;//现在时间
DateTime DateTime1 = fri.LastWriteTime;
//DateTime1 = Convert.ToDateTime(); //设置要求的减的时间
string dateDiff = null;
TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
TimeSpan ts = ts1.Subtract(ts2).Duration(); //显示时间
dateDiff = ts.Days.ToString() + "天" + ts.Hours.ToString() + "小时" + ts.Minutes.ToString() + "分钟" + ts.Seconds.ToString() + "秒";
Console.WriteLine(dateDiff);
Console.WriteLine(fri.Name);
if (ts.Days > 10)
{
Console.WriteLine("timeout!,it has been deleted");
fri.Delete();
}
}
foreach (DirectoryInfo dri in diArr)
{
Console.WriteLine(dri.Name);
}
int count = di.GetFiles().Length;
int count2 = di.GetDirectories().Length;
if (count == 0 && count2 == 0)
{
Console.WriteLine("no files and folders! it has been deleted");
di.Delete();
}
Console.WriteLine("files: " + count);
Console.WriteLine("folder: " + count2);
}
}
}