Regularly Delete temporary files generated by the server

Source: Internet
Author: User

Some temporary files are often generated during import and export. We usually create a temporary folder to store these temporary files,

However, these temporary files will increase over time, so it is necessary to clear them regularly.

The idea is relatively simple. First, create a timer, which regularly executes a method to delete temporary files.

Create a timer in global. asax and call the delete temporary file method in the deltempfile custom class.

View code

1 system. Timers. Timer _ deletetempfile;
2
3 void application_start (Object sender, eventargs E)
4 {
5 // code that runs when the application starts
6 _ deletetempfile = new system. Timers. Timer (1000*60*1 );
7 _ deletetempfile. elapsed + = new system. Timers. elapsedeventhandler (ontimeevent_deltempfile );
8 _ deletetempfile. Enabled = true;
9
10}
11
12 Void ontimeevent_deltempfile (Object sender, system. Timers. elapsedeventargs E)
13 {
14 deltempfile. delete_tempfile (appdomain. currentdomain. basedirectory + "temp_down", 0 ,"*");
15 _ deletetempfile. interval = 1000*60*5;
16 _ deletetempfile. Enabled = true;
17}

To delete a temporary file in a custom class, the Code is as follows:

View code

1 /// <summary>
2 // Delete temporary files
3 /// </Summary>
4 /// <Param name = "path"> path of the deleted file </param>
5 // <Param name = "date"> delete a file before a certain time point </param>
6 /// <Param name = "filetype"> file type </param>
7 public static void delete_tempfile (string path, int datepoint, string filetype)
8 {
9 system. Io. directoryinfo dirinfo = new system. Io. directoryinfo (PATH );
10 datetime time = datetime. Now. adddays (-datepoint );
11
12 // delete a temporary file that exists in the directory
13 if (dirinfo. exists & dirinfo. getfiles (). length> 0)
14 {
15 system. Io. fileinfo [] files = dirinfo. getfiles ();
16
17 For (INT I = 0; I <files. length; I ++)
18 {
19 if (files [I]. exists)
20 {
21 try
22 {
23 if (filetype = "*")
24 {
25 // set this file as a temporary file
26 system. Io. file. setattributes (path + "\" + files [I]. Name, system. Io. fileattributes. Temporary );
27 // Delete the specified object
28 system. Io. file. Delete (path + "\" + files [I]. Name );
29}
30 else
31 {
32 If (files [I]. extension. Equals ("." + filetype ))
33 {
34 // set this file as a temporary file
35 system. Io. file. setattributes (path + "\" + files [I]. Name, system. Io. fileattributes. Temporary );
36 // Delete the specified object
37 system. Io. file. Delete (path + "\" + files [I]. Name );
38}
39 else
40 {
41 continue;
42}
43}
44}
45 catch (system. Io. ioexception ex)
46 {
47 throw ex;
48}
49}
50}
51}
52}

------------------------------------------------------------

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.