添加项目文件。
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace JY.Inspection
|
||||
{
|
||||
public class ServiceLog
|
||||
{
|
||||
public void Start()
|
||||
{
|
||||
Thread thread = new Thread(Init);
|
||||
thread.IsBackground = true;
|
||||
thread.Start();
|
||||
}
|
||||
|
||||
private void Init()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
try
|
||||
{
|
||||
DeleteFile(System.Environment.CurrentDirectory + @"\Logs\", 30); //删除该目录下 超过 30天的文件
|
||||
}
|
||||
catch (Exception err)
|
||||
{
|
||||
Console.WriteLine(err.Message, err.StackTrace);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Thread.Sleep(100000);
|
||||
}
|
||||
}
|
||||
}
|
||||
private void DeleteFile(string fileDirect, int saveDay)
|
||||
{
|
||||
try
|
||||
{
|
||||
DateTime nowTime = DateTime.Now;
|
||||
string[] files = Directory.GetFiles(fileDirect, "*.txt", SearchOption.AllDirectories); //获取该目录下所有 .txt文件
|
||||
foreach (string file in files)
|
||||
{
|
||||
FileInfo fileInfo = new FileInfo(file);
|
||||
TimeSpan t = DateTime.Now - fileInfo.CreationTime; //当前时间 减去 文件创建时间
|
||||
int day = t.Days;
|
||||
if (day > saveDay) //保存的时间,单位:天
|
||||
{
|
||||
if (IsOccupy(fileInfo.FullName)) //判断文件是否被占用
|
||||
{
|
||||
System.IO.File.Delete(fileInfo.FullName); //删除文件
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
Log4Helper.WriteLog("文件被占用,无法操作!","错误提示");
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception err)
|
||||
{
|
||||
Log4Helper.WriteLog("文件被占用,无法操作!",err);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
[DllImport("kernel32.dll")]
|
||||
public static extern IntPtr _lopen(string lpPathName, int iReadWrite);
|
||||
|
||||
[DllImport("kernel32.dll")]
|
||||
public static extern bool CloseHandle(IntPtr hObject);
|
||||
|
||||
public const int OF_READWRITE = 2;
|
||||
public const int OF_SHARE_DENY_NONE = 0x40;
|
||||
public readonly IntPtr HFILE_ERROR = new IntPtr(-1);
|
||||
|
||||
/// <summary>
|
||||
/// 判断文件是否被占用
|
||||
/// </summary>
|
||||
/// <param name="file"></param>
|
||||
/// <returns></returns>
|
||||
private bool IsOccupy(string file)
|
||||
{
|
||||
bool result = true; //默认状态此文件未被占用
|
||||
try
|
||||
{
|
||||
//string vFileName = @"c:\temp\temp.bmp";
|
||||
string vFileName = file;
|
||||
if (!System.IO.File.Exists(vFileName))
|
||||
{
|
||||
//Logger.Info("文件都不存在!");
|
||||
result = false;
|
||||
}
|
||||
IntPtr vHandle = _lopen(vFileName, OF_READWRITE | OF_SHARE_DENY_NONE);
|
||||
if (vHandle == HFILE_ERROR)
|
||||
{
|
||||
|
||||
Log4Helper.WriteLog("文件被占用!", "错误提示");
|
||||
result = false;
|
||||
}
|
||||
CloseHandle(vHandle);
|
||||
Log4Helper.WriteLog("没有被占用!", "错误提示");
|
||||
|
||||
|
||||
}
|
||||
catch (Exception err)
|
||||
{
|
||||
result = false;
|
||||
Log4Helper.WriteLog("判断文件是否被占用", err);
|
||||
|
||||
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user