在广电服务器创建一个FTP目录

时间:2021-12-14 05:27:53

C# /TaskScheduler /按时任务 /按时自动下载 3410 实现道理,客户是广电,在广电处事器创建一个FTP目录,,然后每天自动从卫星上自动更新节目列表, 然后成果就是要每天定点一个时间自动跟FTP目录中前一天下载下来的节目信息进行时间和巨细比拟,如果有差异则自动下载笼罩,让插入到数据库中,因此就想到了两个步伐: 1:用处事来实现 2:windows按时任务 颠末一段时间不雅察看用处事来实现的 下载时间会越来越靠后,觉得不靠谱就想用按时任务来替换。 最终使用 Microsoft.Win32.TaskScheduler 来实现了可以使用,此外两个 FluentScheduler Quartz.Net 来实现, 效果图: 代码: 新建一个控制台应用措施: 然后写代码: using Microsoft.Win32.TaskScheduler; 第一种要领: [csharp] view plain copy // Get the service on the local machine TaskService ts = new TaskService(); // Create a new task definition and assign properties TaskDefinition td = ts.NewTask(); td.RegistrationInfo.Description = "wei kong"; // Add a trigger that will fire the task at this time every other day DailyTrigger dt = (DailyTrigger)td.Triggers.Add(new DailyTrigger { DaysInterval = 1 }); //设置任务起始时间 dt.StartBoundary = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd 09:40:00")); //设置反复任务间隔时间 dt.Repetition.Interval = TimeSpan.FromDays(1); // Create an action that will launch Notepad whenever the trigger fires td.Actions.Add(new ExecAction(@"C:\AutoDownTvInfo.exe", "", null));//这里可参考下载 // Register the task in the root folder ts.RootFolder.RegisterTaskDefinition(@"Test", td); Thread.Sleep(2000); Console.WriteLine("打算任务结束" + DateTime.Now); Thread.Sleep(2000); 第二种要领: 使用要领: var compath = AppDomain.CurrentDomain.BaseDirectory +"AutoDownTvInfo.exe";//这里可参考下载 CreateTaskScheduler("kong wei", "AutoDownTvInfo", compath, "500"); [csharp] view plain copy /// delete task /// /// private static void DeleteTask(string taskName) { TaskSchedulerClass ts = new TaskSchedulerClass(); ts.Connect(null, null, null, null); ITaskFolder folder = ts.GetFolder("\\"); folder.DeleteTask(taskName, 0); } /// /// get all tasks /// public static IRegisteredTaskCollection GetAllTasks() { TaskSchedulerClass ts = new TaskSchedulerClass(); ts.Connect(null, null, null, null); ITaskFolder folder = ts.GetFolder("\\"); IRegisteredTaskCollection tasks_exists = folder.GetTasks(1); return tasks_exists; } /// /// check task isexists /// /// /// public static bool IsExists(string taskName) { var isExists = false; IRegisteredTaskCollection tasks_exists = GetAllTasks(); for (int i = 1; i <= tasks_exists.Count; i++) { IRegisteredTask t = tasks_exists[i]; if (t.Name.Equals(taskName)) { isExists = true; break; } } return isExists; } /// /// create task /// /// /// /// /// /// public static _TASK_STATE CreateTaskScheduler(string creator, string taskName, string path, string interval) { try { if (IsExists(taskName)) { DeleteTask(taskName); } //new scheduler TaskSchedulerClass scheduler = new TaskSchedulerClass(); //pc-name/ip,username,domain,password scheduler.Connect(null, null, null, null); //get scheduler folder ITaskFolder folder = scheduler.GetFolder("\\"); //set base attr ITaskDefinition task = scheduler.NewTask(0); task.RegistrationInfo.Author = "kong wei";//creator task.RegistrationInfo.Description = "每天自动按时下载节目信息";//description //set trigger (IDailyTrigger ITimeTrigger) 设置小时/分/秒来实现 // ITimeTrigger tt = (ITimeTrigger)task.Triggers.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_TIME); // tt.Repetition.Interval = interval;// format PT1H1M==1小时1分钟 设置的值最终城市转身分钟插手到触发器 //设置每天特定的时间来实现 IDailyTrigger tt = (IDailyTrigger)task.Triggers.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_DAILY); tt.StartBoundary = "2016-11-15T10:15:00"; //start time //set action IExecAction action = (IExecAction)task.Actions.Create(_TASK_ACTION_TYPE.TASK_ACTION_EXEC); action.Path = path; task.Settings.ExecutionTimeLimit = "PT0S"; //运行任务时间超时遏制任务吗? PTOS 不开启超时 task.Settings.DisallowStartIfOnBatteries = false;//只有在交流电源下才执行 task.Settings.RunOnlyIfIdle = false;//仅当计算机空闲下才执行 IRegisteredTask regTask = folder.RegisterTaskDefinition(taskName, task, (int)_TASK_CREATION.TASK_CREATE, null, //user null, // password _TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN, ""); IRunningTask runTask = regTask.Run(null); return runTask.State; } catch (Exception ex) { throw ex; } } 参考资料: 原文链接: