简体   繁体   English

在哪里定义脚本以删除早于7天的文件

[英]Where to define scripts that deletes files older then 7 days

I need to write script that delete files older than 7 days in MVC .Net application. 我需要编写脚本来删除MVC .Net应用程序中7天以上的文件。 I'm going to use this code 我将使用此代码

   List<string> DeletePath = new List<string>();
        DirectoryInfo info = new DirectoryInfo(Server.MapPath("~\\TempFiles"));
        FileInfo[] files = info.GetFiles().OrderBy(p => p.CreationTime).ToArray();
        foreach (FileInfo file in files)
        {
            DateTime CreationTime = file.CreationTime;
            double days = (DateTime.Now - CreationTime).TotalDays;
            if (days > 7)
            {
                string delFullPath = file.DirectoryName + "\\" + file.Name;
                DeletePath.Add(delFullPath);
            }
        }
        foreach (var f in DeletePath)
        {
            if (File.Exists(F))
            {
                File.Delete(F);
            }
        }

But i don't know where to define this and how to call. 但是我不知道在哪里定义这个以及如何调用。 Do i need to create new Controller or something similar? 我需要创建新的Controller或类似的东西吗?

wrap your code in a static method and use hangfire 将代码包装在静态方法中并使用hangfire

http://docs.hangfire.io/en/latest/background-methods/performing-recurrent-tasks.html http://docs.hangfire.io/en/latest/background-methods/performing-recurrent-tasks.html

RecurringJob.AddOrUpdate(() => myCleanup.CleanupOldFiles(), Cron.Hourly)

Well you need to create a window service which will run in background on the server. 那么,您需要创建一个window service ,该window service将在服务器的后台运行。 Window services are the normal application which run automatically without any user event. 窗口服务是普通应用程序,可以自动运行而不会发生任何用户事件。 Create a window service, then you can write the same code there wrapped in a timer tick event . 创建一个窗口服务,然后您可以在其中将相同的代码写入timer tick event There you can set the time when you want to execute this code. 您可以在此处设置执行此代码的时间。

Usually window service once deployed start executing your code every second. 通常,一旦部署窗口服务,就会每秒开始执行代码。 So you need to set the clock. 因此,您需要设置时钟。 With the Timer, you can execute your code as per your requirement, everyday at particular time, once in a week, once in a month or so on so forth. 使用计时器,您可以每天在特定时间,每周一次,每月一次等等按要求执行代码。

Read more about window services here... 在此处阅读有关窗口服务的更多信息...

and let me know if you need code for this. 并告诉我您是否需要此代码。

Mvc is not the job for this. Mvc不是这个工作。 It's like asking how to install tires on a train so that it can drive up a private street. 这就像问如何在火车上安装轮胎,以便可以沿着一条私人街道行驶。

In order for Mvc or any web application for that matter to do something, a request must come in from a client. 为了使Mvc或任何与此有关的Web应用程序能够执行某些操作,必须从客户端发出请求。 If no request comes in, IIS does nothing and just "listens" for incoming connections. 如果没有请求传入,则IIS不执行任何操作,仅“侦听”传入的连接。 To do this the "correct" way, you have two options: 要以“正确”的方式执行此操作,您有两个选择:

  1. Create a batch file or Powershell script or even a C# console application that deletes the files, then setup that batch file or script or program to run in Windows Task Scheduler . 创建一个批处理文件Powershell脚本 ,甚至创建一个删除文件的C#控制台应用程序 ,然后设置该批处理文件或脚本或程序以在Windows Task Scheduler中运行。

  2. If you need more logic to determine when the files should be deleted or you need customized schedules, then you should make a Windows Service Application . 如果您需要更多的逻辑来确定何时删除文件或需要自定义的计划,则应制作Windows服务应用程序 You can make this in C# and you can pretty much have it do anything you want, so long as it doesn't need a GUI. 您可以使用C#进行此操作,只要它不需要GUI,就可以使它执行您想做的任何事情。 If you do need a GUI, perhaps to configure the service, then you can make a separate WinForms or WPF application that can configure the service. 如果确实需要GUI(也许是为了配置服务),则可以制作一个单独的WinForms或WPF应用程序来配置服务。 There are several ways to implement GUI/Service communication. 有几种方法可以实现GUI /服务通信。 Some of the more popular ones are WCF communication, database configuration or even INI files. WCF通讯,数据库配置甚至INI文件都是一些比较流行的文件。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM