c#文件助手类分享(读取文件内容 操作日志文件)
本文导语: 代码如下:using System;using System.Collections.Generic;using System.IO;using System.Text.RegularExpressions;using System.Linq;using System.Runtime.InteropServices;namespace Humep.FileWatcher{ /// /// 日志文件操作 /// Date:2011-06-01 /// public static clas...
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using System.Linq;
using System.Runtime.InteropServices;
namespace Humep.FileWatcher
{
///
/// 日志文件操作
/// Date:2011-06-01
///
public static class FileHelper
{
///
/// 获取文件路径中最后的目录名
///
///
///
public static string GetDirectoryName(string fullName)
{
if (string.IsNullOrWhiteSpace(fullName))
{
return null;
}
return fullName.Substring(0,fullName.LastIndexOf('\')+1);
}
///
/// 获取路径中的文件名称
///
///
///
public static string GetFileName(string filePath)
{
if (string.IsNullOrWhiteSpace(filePath))
{
return string.Empty;
}
if (filePath.Length > 260)
{
return filePath.Substring(filePath.LastIndexOf('\') + 1, int.MaxValue);
}
return Path.GetFileName(filePath);
}
///
/// 文件名是否满足filePattern格式。
///
///
public static bool IsMatched(string fileName, string filePattern)
{
if (string.IsNullOrWhiteSpace(fileName))
{
return false;
}
if (string.IsNullOrWhiteSpace(filePattern))
{
return false;
}
Regex regex = new Regex(filePattern, RegexOptions.IgnoreCase);
return regex.IsMatch(fileName);
}
///
/// 读取文件内容
///
///
///
public static string ReadAllText(string filePath)
{
if (string.IsNullOrWhiteSpace(filePath) || File.Exists(filePath) == false)
{
return string.Empty;
}
return File.ReadAllText(filePath);
}
///
/// 删除文件
///
///
public static bool Delete(string filePath)
{
if (string.IsNullOrWhiteSpace(filePath))
{
return false;
}
if (!File.Exists(filePath))
{
return false;
}
File.Delete(filePath);
return !File.Exists(filePath);
}
///
/// 删除目录下所有过期文件
///
///
///
public static int ClearExpiredFiles(string directory, int expiredDays)
{
if (!Directory.Exists(directory))
{
return 0;
}
if (expiredDays 260)
{
return MoveLongPathFile(@"\?"+sourcePath,@"\?"+ targetPath);
}
File.Move(sourcePath, targetPath);
return true;
}
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true, EntryPoint = "MoveFile")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool MoveLongPathFile(string lpExistingFileName, string lpNewFileName);
///
/// 单个文件移动
///
///
///
///
///
public static bool MoveFile(string fileName, string sourceDirectory, string targetDirectory)
{
if (!File.Exists(fileName))
{
return false;
}
if (!Directory.Exists(sourceDirectory))
{
return false;
}
if (!Directory.Exists(targetDirectory))
{
return false;
}
string filePath = fileName.Replace(sourceDirectory, targetDirectory);
string fileDir=Path.GetDirectoryName(filePath);
if(!DirectoryHelper.CreateDirectory(fileDir))
{
return false;
}
return MoveFile(fileName, fileDir);
}
///
/// 重新生成新的文件路径
///
///
///
public static string Rename(string filePath)
{
if (string.IsNullOrWhiteSpace(filePath))
{
return string.Empty;
}
string lastFileName = Path.GetFileNameWithoutExtension(filePath);
string lastFileExtension = Path.GetExtension(filePath);
//重命名,则随机在原来文件名后面加几个随机数字进行组装成新的名字
Random random = new Random(System.DateTime.Now.Millisecond);
string randomData = random.Next().ToString();
//把原文件名的名字加上随机数,组装成新的文件名(避免重名)
string newFileName = lastFileName + randomData;
return Path.GetDirectoryName(filePath) + "\" + newFileName + lastFileExtension;
}
}
}