您的位置:首页 > 其它

.net定期删除一文件夹下的所有文件,并调用相关exe

2011-03-25 11:21 375 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO;

namespace DeleteFile
{
    /// <summary>
    ///  /bin/Debug下有2个文件,一个保存每次删除文件夹的日期,另一个保存被调用的exe路径
    /// 
    ///  此代码实现的功能:每6天删除文件夹下的所有文件【直接删除文件夹】,然后创建此文件夹,并且调用相关exe,如果时间不到,则只调用exe
    /// </summary>
    class Program
    {
        //要删除的目录路径
        string path = "E://cap";

        //保存日期的文件目录
        string debugPath = AppDomain.CurrentDomain.BaseDirectory;

        static void Main(string[] args)
        {
            new Program().invoke();
        }

        void invoke()
        {
            //获取保存日期的文件的路径
            string saveDatePath = debugPath + "saveDateFile.txt";
            //获取保存被调用的exe的路径
            string exePath = File.ReadAllText(debugPath + "saveExe.txt");

            //获取当前日期
            DateTime nowDate = DateTime.Today;

            //获取上次日期
            string last = File.ReadAllText(saveDatePath);

            Console.WriteLine("当前日期:" + nowDate.ToShortDateString() + "/n上次保存的日期: " + last);
            DateTime lastDate = Convert.ToDateTime(last);

            //计算差值
            TimeSpan timeSpan = nowDate - lastDate;
            double minus = timeSpan.TotalDays;

            Console.WriteLine("差值: " + minus);

            if (minus >= 6)
            {
                Console.WriteLine("一个星期。。删除....");
                if (Directory.Exists(path))
                {
                    Directory.Delete(path, true); //此文件夹下的文件约100G
                    Thread.Sleep(60* 1000);
                    Directory.CreateDirectory(path);

                    File.WriteAllText(saveDatePath, nowDate.ToShortDateString());

                    Console.WriteLine("新日期已成功存入文件中...");
                    System.Diagnostics.Process.Start(exePath);
                }
                else
                {
                    Console.WriteLine("E://cap目录不存在...");
                }
            }
            else
            {             
                Thread.Sleep(5 * 1000);
                System.Diagnostics.Process.Start(exePath);
            }
        }
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: