84 lines
3.9 KiB
C#
84 lines
3.9 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Text;
|
||
using System.IO;
|
||
using System.Windows.Forms;
|
||
namespace PayForApp.Business
|
||
{
|
||
public class LogHelper
|
||
{
|
||
|
||
public static string path = Application.StartupPath;
|
||
public static void WriteServiceLog(string LogData)
|
||
{
|
||
LogData = DateTime.Now.ToLongTimeString()+":" + LogData;
|
||
string LogName = "日志_Send"+System.DateTime.Now.Year.ToString() + "-" + System.DateTime.Now.Month.ToString() + "-" + System.DateTime.Now.Day.ToString()+".log";
|
||
string filename = path + "\\log\\" + LogName;
|
||
if (!Directory.Exists(path + "\\log\\"))
|
||
Directory.CreateDirectory(path + "\\log\\");
|
||
FileStream fs = new FileStream(filename, FileMode.OpenOrCreate);
|
||
fs.Seek(0, SeekOrigin.End);
|
||
byte[] _data = new UTF8Encoding().GetBytes(LogData+"\r\n");
|
||
fs.Write(_data, 0, _data.Length);
|
||
fs.Flush();
|
||
fs.Close();
|
||
}
|
||
|
||
public static void WriteServiceLog2(string LogData)
|
||
{
|
||
LogData = DateTime.Now.ToLongTimeString() + ":" + LogData;
|
||
string LogName = "日志_Receive" + System.DateTime.Now.Year.ToString() + "-" + System.DateTime.Now.Month.ToString() + "-" + System.DateTime.Now.Day.ToString() + ".log";
|
||
string filename = path + "\\" + LogName;
|
||
FileStream fs = new FileStream(filename, FileMode.OpenOrCreate);
|
||
fs.Seek(0, SeekOrigin.End);
|
||
byte[] _data = new UTF8Encoding().GetBytes(LogData + "\r\n");
|
||
fs.Write(_data, 0, _data.Length);
|
||
fs.Flush();
|
||
fs.Close();
|
||
|
||
}
|
||
/// <summary>
|
||
/// 删除指定目录下的指定后缀名的文件
|
||
/// </summary>
|
||
/// <param name="directory">要删除的文件所在的目录,是绝对目录,如d:\temp</param>
|
||
/// <param name="masks">要删除的文件的后缀名的一个数组,比如masks中包含了.cs,.vb,.c这三个元素</param>
|
||
/// <param name="searchSubdirectories">表示是否需要递归删除,即是否也要删除子目录中相应的文件</param>
|
||
/// <param name="ignoreHidden">表示是否忽略隐藏文件</param>
|
||
/// /// <param name="deletedFileCount">表示总共删除的文件数</param>
|
||
public static void DeleteFiles(string directory, string[] masks, bool searchSubdirectories, bool ignoreHidden, ref int deletedFileCount)
|
||
{
|
||
//先删除当前目录下指定后缀名的所有文件
|
||
foreach (string file in Directory.GetFiles(directory, "*.*"))
|
||
{
|
||
if (!(ignoreHidden && (File.GetAttributes(file) & FileAttributes.Hidden) == FileAttributes.Hidden))
|
||
{
|
||
foreach (string mask in masks)
|
||
{
|
||
if (Path.GetExtension(file) == mask)
|
||
{
|
||
FileInfo _FileInfo = new FileInfo(file);
|
||
if (_FileInfo.LastWriteTime < DateTime.Now.Date.AddDays(-10))
|
||
{
|
||
_FileInfo.Delete();
|
||
deletedFileCount++;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
//如果需要对子目录进行处理,则对子目录也进行递归操作
|
||
if (searchSubdirectories)
|
||
{
|
||
string[] childDirectories = Directory.GetDirectories(directory);
|
||
foreach (string dir in childDirectories)
|
||
{
|
||
if (!(ignoreHidden && (File.GetAttributes(dir) & FileAttributes.Hidden) == FileAttributes.Hidden))
|
||
{
|
||
DeleteFiles(dir, masks, searchSubdirectories, ignoreHidden, ref deletedFileCount);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|