92 lines
3.3 KiB
C#
92 lines
3.3 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.IO;
|
|||
|
using System.Linq;
|
|||
|
using System.Threading.Tasks;
|
|||
|
|
|||
|
namespace ShowTrend3.Libs
|
|||
|
{
|
|||
|
public static class TempDir
|
|||
|
{
|
|||
|
//private const char Slash = '\\';
|
|||
|
private const char Slash = '/';
|
|||
|
public enum Status { free, write, read };
|
|||
|
private static Dictionary<string, int> readFile = new Dictionary<string, int>();
|
|||
|
private static List<string> writeFile = new List<string>();
|
|||
|
|
|||
|
public static void Clear()
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
var tempDir = Directory.GetCurrentDirectory() + Slash + "temp";
|
|||
|
if (!Directory.Exists(tempDir))
|
|||
|
Directory.CreateDirectory(tempDir);
|
|||
|
string[] files = Directory.GetFiles(tempDir, "*");
|
|||
|
foreach (var e in files)
|
|||
|
{
|
|||
|
if (readFile.ContainsKey(e.Split(Slash).Last()) || writeFile.Contains(e.Split(Slash).Last()))
|
|||
|
continue;
|
|||
|
|
|||
|
DateTime createDate = File.GetCreationTime(e);
|
|||
|
bool flag = createDate.Year == DateTime.Now.Year &&
|
|||
|
createDate.Month == DateTime.Now.Month &&
|
|||
|
createDate.Day == DateTime.Now.Day &&
|
|||
|
(DateTime.Now - createDate).TotalMinutes > 2;
|
|||
|
flag |= createDate.Day == DateTime.Now.AddDays(-1).Day &&
|
|||
|
DateTime.Now.Hour == 0 &&
|
|||
|
(DateTime.Now - createDate).TotalMinutes > 2;
|
|||
|
flag |= (DateTime.Now - createDate).TotalHours > 1;
|
|||
|
if (flag)
|
|||
|
File.Delete(e);
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception e)
|
|||
|
{
|
|||
|
Console.WriteLine(e.Message);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public static bool CheckFileExist(string onlyName)
|
|||
|
{
|
|||
|
return writeFile.Contains(onlyName) ||
|
|||
|
File.Exists(Directory.GetCurrentDirectory() + Slash + "temp" + Slash + onlyName);
|
|||
|
}
|
|||
|
|
|||
|
public static FileStream StartWrite(string onlyName)
|
|||
|
{
|
|||
|
writeFile.Add(onlyName);
|
|||
|
return new FileStream(Directory.GetCurrentDirectory() + Slash + "temp" + Slash + onlyName, FileMode.CreateNew);
|
|||
|
}
|
|||
|
public static void EndWrite(string onlyName)
|
|||
|
{
|
|||
|
writeFile.Remove(onlyName);
|
|||
|
}
|
|||
|
|
|||
|
public static void LockRead(string onlyName)
|
|||
|
{
|
|||
|
if (!readFile.ContainsKey(onlyName))
|
|||
|
readFile.Add(onlyName, 0);
|
|||
|
readFile[onlyName]++;
|
|||
|
}
|
|||
|
public static bool CanRead(string onlyName)
|
|||
|
{
|
|||
|
if (!CheckFileExist(onlyName))
|
|||
|
return false;
|
|||
|
return !writeFile.Contains(onlyName);
|
|||
|
}
|
|||
|
public static FileStream StartRead(string onlyName)
|
|||
|
{
|
|||
|
return new FileStream(Directory.GetCurrentDirectory() + Slash + "temp" + Slash + onlyName, FileMode.Open);
|
|||
|
}
|
|||
|
public static void FreeRead(string onlyName)
|
|||
|
{
|
|||
|
if (!readFile.ContainsKey(onlyName))
|
|||
|
return;
|
|||
|
if (readFile[onlyName] > 0)
|
|||
|
readFile[onlyName]--;
|
|||
|
if (readFile[onlyName] == 0)
|
|||
|
readFile.Remove(onlyName);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|