private static void analysisDeleteConfig(string[] lines, int line)
        {
            int flag = 0, index = 0, i = line;
            DeleteConfigInfo infor = new DeleteConfigInfo();

            for (; i < lines.Length; i++)
            {
                index = lines[i].IndexOf(":") + 1;
                if (lines[i].Length > 4 && index != -1)
                {
                    switch (lines[i].Substring(0, 2))
                    {
                    case "FP": infor.setPath(lines[i].Substring(index, lines[i].Length - index)); flag += 2; break;

                    case "SL": infor.setDeleteLimit(lines[i].Substring(index, lines[i].Length - index)); flag--; break;

                    default: break;
                    }
                }
                if (flag == 1)
                {
                    GlobleParams.configListAdd(infor);
                    infor = new DeleteConfigInfo();
                    flag  = 0;
                }
                else if (flag == -1)
                {
                    break;
                }
            }
        }
        /**
         * 初始化配置文件
         * */
        private static void intiConfig()
        {
            string filePath            = GlobleParams.getConfigPath();
            string initialConfigString = getInitialConfigString();

            writeConfig(filePath, initialConfigString);
        }
        /**
         * 写日志
         * */
        public static void writeLog(string log)
        {
            StreamWriter sw = new StreamWriter(GlobleParams.getLogPath(), true, Encoding.GetEncoding("gb2312"));

            sw.WriteLine(log);
            sw.Close();
            sw.Dispose();
        }
        /**
         * 执行定时任务
         * */
        private void startTimeJob()
        {
            Timer timer = new Timer();

            //执行时间间隔
            timer.Interval = GlobleParams.getFrequency();
            timer.Elapsed += new ElapsedEventHandler(CleanWorker.work);
            timer.Start();
        }
Exemple #5
0
        /**
         * 入口程序
         * */
        public static void work(object sender, ElapsedEventArgs args)
        {
            //检查日志
            LogFileOperator.check();
            //执行文件删除
            string log = Cleaner.cleanUpExcessFiles(GlobleParams.getConfigList(), GlobleParams.getDeletePercent());

            //写日志
            LogFileOperator.writeLog(log);
        }
        /**
         * 检查配置文件是否需要初始化
         * 配置文件为空则认为需要初始化
         * */
        private static bool needToInit()
        {
            string filePath = GlobleParams.getConfigPath();

            if (File.ReadAllLines(filePath, Encoding.GetEncoding("gb2312")).Length == 0)
            {
                return(true);
            }
            return(false);
        }
        private static void printGlobleParams()
        {
            StringBuilder log = new StringBuilder();

            log.AppendLine("当前时间:" + DateTime.Now.ToString() + " 服务启动...\n");
            log.AppendLine("执行频率:" + GlobleParams.getFrequency() / 60000 + "分钟");
            log.AppendLine("删除比例:" + GlobleParams.getDeletePercent() + "%");
            log.AppendLine("共读取到:" + GlobleParams.getConfigList().Count() + "个需要检查的路径\n");
            LogFileOperator.writeLog(log.ToString());
        }
        public static void read()
        {
            string path = GlobleParams.getConfigPath();

            //读取配置文件初始化数据
            string[] lines = File.ReadAllLines(path);
            int      line  = analysisPublicData(lines);

            analysisDeleteConfig(lines, line);
            createGlobleParams();
        }
        /**
         * 检查日志文件及文件夹是否存在,不存在则创建
         * */
        public static void check()
        {
            string logDirectory = GlobleParams.getLogDirectory();
            string filePath     = GlobleParams.getLogPath();

            if (!Directory.Exists(logDirectory))
            {
                Directory.CreateDirectory(logDirectory);
            }
            if (!File.Exists(filePath))
            {
                File.Create(filePath).Dispose();
            }
        }
        /**
         * 检查配置文件夹和配置文件是否存在
         * 若不存在则创建
         * */
        private static void createIfIsNotExist()
        {
            string configDirectory = GlobleParams.getConfigDirectory();
            string filePath        = GlobleParams.getConfigPath();

            if (!Directory.Exists(configDirectory))
            {
                Directory.CreateDirectory(configDirectory);
            }
            if (!File.Exists(filePath))
            {
                File.Create(filePath).Dispose();
            }
        }
        /**
         * 解析公用数据(执行频率和删除百分比)
         * */
        private static int analysisPublicData(string[] lines)
        {
            int flag = 0, i = 0;

            for (; i < lines.Length && flag < 2; i++)
            {
                if (lines[i].Length > 3)
                {
                    switch (lines[i].Substring(0, 3))
                    {
                    case "LP:": GlobleParams.setLogPath(lines[i].Substring(3, lines[i].Length - 3)); break;

                    case "TF:": GlobleParams.setFrequency(lines[i].Substring(3, lines[i].Length - 3)); flag++; break;

                    case "DP:": GlobleParams.setDeletePercent(lines[i].Substring(3, lines[i].Length - 3)); flag++; break;

                    default: break;
                    }
                }
            }
            return(i);
        }
 private static void createGlobleParams()
 {
     GlobleParams.setFrequency(GlobleParams.getFrequency() == -60000 ? "30" : (GlobleParams.getFrequency() / 60000).ToString());
     GlobleParams.setDeletePercent(GlobleParams.getDeletePercent() == -1 ? "10" : GlobleParams.getDeletePercent().ToString());
 }