C#操作计划任务

    昨天有一个任务,就是要下载相关文件,然后保存在相关路径下,这个没什么难度,所以就略过不谈,主要谈谈定时下载,即每天某个固定时间执行下载,这个功能我是用C#代码来操作windows自带的任务计划来实现的,所以我简单的写了一个任务计划操作类,以下是代码主体部分,基本上可实现我需要的功能,当然,因为时间仓促,还不是很完善,我会尽量抽时间完善这个公共类,以便以后操作的时候,可以直接使用,废话说完,开始进入正题:

1,首先下载TaskScheduler.dll,然后将其添加到引用中。

2,然后下面是TaskSchedulerHandler.cs的代码,也就是满足我的需要的操作类

    /// <summary>
    /// @Date:12/10/2012
    /// @Author:Andy
    /// @Desciption:class of add,delete and select Task Scheduler
    /// </summary>
    public class TaskSchedulerHandler
    {
        public string hour;//task hour
        public string minute;//task minute
        public string interval;//task interval
        public string taskName;//Name of Task
        public string userName;//user to authorize the task
        public string userPwd;//password of user

        public TaskSchedulerHandler()
        {
            InitVariables();
        }

        /// <summary>
        /// Add Task to TaskScheduler
        /// </summary>
        public void AddTask()
        {
            if (!SelTask())
            {
                ScheduledTasks st = new ScheduledTasks();
                Task autoDownload = st.CreateTask(taskName);
                DailyTrigger dayTrigger = new DailyTrigger(Convert.ToInt16(hour), Convert.ToInt16(minute), Convert.ToInt16(interval));//get default config from app.config

                autoDownload.Triggers.Add(dayTrigger);
                //autoDownload.SetAccountInformation(userName, userPwd);//to authorize the console ro run
                autoDownload.ApplicationName = Assembly.GetExecutingAssembly().Location;
                
                //you can also modify the values in app.config 
                //and publish it, then you can add the application name here
                //autoDownload.ApplicationName = "";
                autoDownload.Save();
                autoDownload.Close();
            }
            //else
            //{
            //    DelTask(taskName);
            //    AddTask();
            //}
        }

        /// <summary>
        /// Select Task from TaskScheduler
        /// </summary>
        /// <param name="taskName"></param>
        /// <returns></returns>
        public bool SelTask()
        {
            bool flag = false;
            ScheduledTasks st = new ScheduledTasks();
            string[] taskNameStr = st.GetTaskNames();
            foreach (string item in taskNameStr)
            {
                if (taskName == item.Split('.')[0])
                {
                    flag = true;
                    break;
                }
            }

            return flag;
        }

        /// <summary>
        /// Delete Task
        /// </summary>
        /// <param name="taskName"></param>
        public void DelTask(string taskName)
        {
            if (taskName != "" && taskName != null)
            {
                ScheduledTasks st = new ScheduledTasks();
                st.DeleteTask(taskName);
            }
        }

        /// <summary>
        /// Set Default Value for Variables
        /// </summary>
        public void InitVariables()
        {
            hour = ConfigurationManager.AppSettings["taskHour"];
            minute = ConfigurationManager.AppSettings["taskMinute"];
            interval = ConfigurationManager.AppSettings["interval"];
            taskName = ConfigurationManager.AppSettings["taskName"];
            userName = ConfigurationManager.AppSettings["taskUser"];
            userPwd = ConfigurationManager.AppSettings["taskPwd"];
        }
    }
这样,一个简单的任务计划操作类就完成了,简单说下,因为目前我的很多的值都需要存储在app.config中,以便日后修改或进行配置,所以我的变量大多是直接读取app.config中的值来实现赋值的,这也是下一步我打算完善的,希望把这个作为一个公共类,可以让其他人直接使用。

PS:自由主义和开源精神的支持者!

posted @ 2012-10-13 16:34  海盗小dj  阅读(221)  评论(0编辑  收藏  举报