MultipartFormDataStreamProvider Cleanup

后端 未结 2 1498
谎友^
谎友^ 2021-02-20 14:59

If files are posted to my webapp, then I read them via MultipartFormDataStreamProvider.FileData.

I Initialize the provider like this:

strin         


        
2条回答
  •  南旧
    南旧 (楼主)
    2021-02-20 15:20

    You could delete all files that are older than a certain timespan. e.g.

    private void CleanTempFiles(string dir, int ageInMinutes)
    {
        string[] files = Directory.GetFiles(dir);
    
        foreach (string file in files)
        {
            var time = File.GetCreationTime(file);
    
            if (time.AddMinutes(ageInMinutes) < DateTime.Now)
            {
                File.Delete(file);
            }
        }
    }
    

    Then call it with something like:

    CleanTempFiles(root, 60); // Delete all files older than 1 hour
    

提交回复
热议问题