101 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			101 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using System;
 | |
| using System.Collections.Generic;
 | |
| using System.Text;
 | |
| using System.IO;
 | |
| using System.Text.RegularExpressions;
 | |
| 
 | |
| namespace Volian.Base.Library
 | |
| {
 | |
| 	public static class TmpFile
 | |
| 	{
 | |
| 		public static string CreateFileName(string procNumber, string sectNumber, string sectTitle)
 | |
| 		{
 | |
| 			return FixFileName(procNumber + "_" + ((sectNumber ?? "") != "" ? sectNumber : sectTitle));
 | |
| 		}
 | |
| 		public static string CreateFileName(string procNumber)
 | |
| 		{
 | |
| 			return FixFileName(procNumber);
 | |
| 		}
 | |
| 		public static string FixFileName(string name)
 | |
| 		{
 | |
| 			return Regex.Replace(name, "[ .,/]", "_") + ".pdf";
 | |
| 		}
 | |
| 		public static void RemoveAllTmps()
 | |
| 		{
 | |
| 			RemoveTmpPDFs();
 | |
| 			RemoveTmpDocs();
 | |
| 			RemoveTmps("*.png");
 | |
| 		}
 | |
| 		public static void RemoveTmpPDFs()
 | |
| 		{
 | |
| 			string tmpFolder = VlnSettings.TemporaryFolder;
 | |
| 			DirectoryInfo di = new DirectoryInfo(tmpFolder);
 | |
| 			if (!di.Exists) return;
 | |
| 			foreach (FileInfo fi in di.GetFiles("*.pdf"))
 | |
| 			{
 | |
| 				try
 | |
| 				{
 | |
| 					// only delete pdfs if they are older than an hour - just to be
 | |
| 					// safe.  During creation of pdf's, separate pdfs are created for
 | |
| 					// word sections and imported into the main resulting pdf.
 | |
| 					if (fi.LastAccessTime.Ticks < (DateTime.Now.Ticks - TimeSpan.TicksPerHour))
 | |
| 						fi.Delete();
 | |
| 				}
 | |
| 				catch (Exception ex)
 | |
| 				{
 | |
| 					continue;   // if an error, go onto next file.
 | |
| 				}
 | |
| 			}
 | |
| 
 | |
| 		}
 | |
| 		public static void RemoveTmpDocs()
 | |
| 		{
 | |
| 			// B2017-249 Recover Temporary File And AutoSave support for MSWord
 | |
| 			// Remove temporary Doc Files more than 2 days old so that files with changes can be recoered.
 | |
| 			RemoveOldTmps("*.doc");
 | |
| 			RemoveOldTmps("*.docx");
 | |
| 			RemoveOldTmps("*.rtf");
 | |
| 		}
 | |
|         private static void RemoveOldTmps(string extension)
 | |
|         {
 | |
|             string tmpFolder = VlnSettings.TemporaryFolder;
 | |
|             DirectoryInfo di = new DirectoryInfo(tmpFolder);
 | |
|             if (!di.Exists) return;
 | |
|             foreach (FileInfo fi in di.GetFiles(extension))
 | |
|             {
 | |
|                 try
 | |
|                 {
 | |
|                     // delete any docs.  If it fails, just go on to the next one because
 | |
|                     // it may be open from another process.
 | |
|                     if(fi.LastWriteTimeUtc < DateTime.Now.AddDays(-2.0))
 | |
|                     fi.Delete();
 | |
|                 }
 | |
|                 catch (Exception ex)
 | |
|                 {
 | |
|                     continue;   // if an error, go onto next file.
 | |
|                 }
 | |
|             }
 | |
|         }
 | |
| 
 | |
| 		private static void RemoveTmps(string extension)
 | |
| 		{
 | |
| 			string tmpFolder = VlnSettings.TemporaryFolder;
 | |
| 			DirectoryInfo di = new DirectoryInfo(tmpFolder);
 | |
| 			if (!di.Exists) return;
 | |
| 			foreach (FileInfo fi in di.GetFiles(extension))
 | |
| 			{
 | |
| 				try
 | |
| 				{
 | |
| 					// delete any docs.  If it fails, just go on to the next one because
 | |
| 					// it may be open from another process.
 | |
| 					fi.Delete();
 | |
| 				}
 | |
| 				catch (Exception ex)
 | |
| 				{
 | |
| 					continue;   // if an error, go onto next file.
 | |
| 				}
 | |
| 			}
 | |
| 		}
 | |
| 	}
 | |
| }
 | 
