檔案助手類
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using System.Linq;
using System.Runtime.InteropServices;
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;
}
{
///
/// 日誌檔案操作
/// 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;
}
/// 獲取路徑中的檔名稱
///
///
///
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 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;
}
{
return false;
}
Regex regex = new Regex(filePattern, RegexOptions.IgnoreCase);
return regex.IsMatch(fileName);
}
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;
}
/// 刪除檔案
///
///
public static bool Delete(string filePath)
{
if (string.IsNullOrWhiteSpace(filePath))
{
return false;
}
if (!File.Exists(filePath))
{
return false;
}
{
return false;
}
File.Delete(filePath);
return !File.Exists(filePath);
}
}
///
/// 刪除目錄下所有過期檔案
///
///
///
public static int ClearExpiredFiles(string directory, int expiredDays)
{
if (!Directory.Exists(directory))
{
return 0;
}
/// 刪除目錄下所有過期檔案
///
///
///
public static int ClearExpiredFiles(string directory, int expiredDays)
{
if (!Directory.Exists(directory))
{
return 0;
}
if (expiredDays <= 0)
{
return 0;
}
{
return 0;
}
DirectoryInfo dir = new DirectoryInfo(directory);
IList fileInfos =dir.GetFiles();
IList
if (fileInfos == null || fileInfos.Count < 1)
{
return 0;
}
{
return 0;
}
int count = 0;
foreach (FileInfo fileInfo in fileInfos)
{
if (fileInfo.CreationTime.AddDays(expiredDays) < DateTime.Now)
{
//added by yangbinggang
fileInfo.Attributes = FileAttributes.Normal;
FileHelper.Delete(fileInfo.FullName);
count = count + 1;
}
}
{
if (fileInfo.CreationTime.AddDays(expiredDays) < DateTime.Now)
{
//added by yangbinggang
fileInfo.Attributes = FileAttributes.Normal;
FileHelper.Delete(fileInfo.FullName);
count = count + 1;
}
}
return count;
}
}
///
/// 刪除目錄下所有過期檔案
///
/// 目錄陣列
///
///
public static int ClearExpiredFiles(string[] dirs, int expiredDays)
{
if (dirs == null)
{
return 0;
}
/// 刪除目錄下所有過期檔案
///
/// 目錄陣列
///
///
public static int ClearExpiredFiles(string[] dirs, int expiredDays)
{
if (dirs == null)
{
return 0;
}
if (dirs.Length <= 0)
{
return 0;
}
{
return 0;
}
int count = 0;
foreach (string dir in dirs)
{
count = count + ClearExpiredFiles(dir, expiredDays);
}
{
count = count + ClearExpiredFiles(dir, expiredDays);
}
return count;
}
}
///
/// 刪除過期目錄及其子目錄和檔案
///
/// 目錄陣列
///
///
public static int ClearExpiredDirectories(string[] directories, int expiredDays)
{
if (directories == null || directories.Length <= 0)
{
return 0;
}
/// 刪除過期目錄及其子目錄和檔案
///
/// 目錄陣列
///
///
public static int ClearExpiredDirectories(string[] directories, int expiredDays)
{
if (directories == null || directories.Length <= 0)
{
return 0;
}
if (expiredDays < 0)
{
return 0;
}
{
return 0;
}
int count = 0;
foreach (string directory in directories)
{
if (!Directory.Exists(directory))
{
continue;
}
{
if (!Directory.Exists(directory))
{
continue;
}
count += ClearExpiredFiles(directory, expiredDays);
DirectoryInfo directoryInfo = new DirectoryInfo(directory);
if (directoryInfo.CreationTime.AddDays(expiredDays) < DateTime.Now)
{
directoryInfo.Attributes = FileAttributes.Normal;
Directory.Delete(directory, true);
}
}
{
directoryInfo.Attributes = FileAttributes.Normal;
Directory.Delete(directory, true);
}
}
return count;
}
}
///
/// 深度列舉出所有子目錄(包括子目錄的子目錄)
///
///
///
public static IList EnumerateAllSubDirectories(string directory)
{
List direcotryList = new List();
/// 深度列舉出所有子目錄(包括子目錄的子目錄)
///
///
///
public static IList
{
List
if (string.IsNullOrWhiteSpace(directory))
{
return direcotryList;
}
{
return direcotryList;
}
if (!Directory.Exists(directory))
{
return direcotryList;
}
{
return direcotryList;
}
string[] folders = Directory.GetDirectories(directory);
direcotryList.AddRange(folders);
foreach (string folder in folders)
{
direcotryList.AddRange(EnumerateAllSubDirectories(folder));
}
return direcotryList;
}
{
direcotryList.AddRange(EnumerateAllSubDirectories(folder));
}
return direcotryList;
}
///
/// 根據時間查詢檔案
///
///
///
///
public static IList FindFiles(string directory, int maxCount, int days)
{
IList fileList = new List();
/// 根據時間查詢檔案
///
///
///
///
public static IList
{
IList
if (!Directory.Exists(directory) || maxCount <= 0)
{
return fileList;
}
{
return fileList;
}
string[] files = Directory.GetFiles(directory);
if(files==null)
{
return fileList;
}
if(files==null)
{
return fileList;
}
//modified by yangbinggang 2012-12-10 DTS2012121004132\DTS2012121004404\DTS2012121004291
DateTime lastTime = DateTime.Now.AddDays(-Math.Abs(days));
DateTime lastTime = DateTime.Now.AddDays(-Math.Abs(days));
fileList = files.Where(item =>
{
if (maxCount <= 0)
{
return false;
}
FileInfo fileInfo = new FileInfo(item);
if (fileInfo.LastWriteTime >= lastTime)
{
maxCount--;
return true;
}
return false;
{
if (maxCount <= 0)
{
return false;
}
FileInfo fileInfo = new FileInfo(item);
if (fileInfo.LastWriteTime >= lastTime)
{
maxCount--;
return true;
}
return false;
}).ToList();
return fileList;
}
return fileList;
}
///
/// 查詢目錄下的所有檔案,將recursive設為True可查詢子目錄中的所有檔案。
///
///
///
///
///
///
public static IList FindFiles(string directory, string filePattern, int maxCount, bool recursive)
{
if (!recursive)
{
return FileHelper.FindFiles(directory, filePattern, maxCount);
}
/// 查詢目錄下的所有檔案,將recursive設為True可查詢子目錄中的所有檔案。
///
///
///
///
///
///
public static IList
{
if (!recursive)
{
return FileHelper.FindFiles(directory, filePattern, maxCount);
}
IList directories = EnumerateAllSubDirectories(directory);
return FindFiles(directories,filePattern, maxCount);
}
return FindFiles(directories,filePattern, maxCount);
}
public static IList FindFiles(IList directories,string filePattern, int maxCount)
{
List files = new List();
foreach (string directoryItem in directories)
{
files.AddRange(FileHelper.FindFiles(directoryItem, filePattern, maxCount));
{
List
foreach (string directoryItem in directories)
{
files.AddRange(FileHelper.FindFiles(directoryItem, filePattern, maxCount));
if (files.Count > maxCount)
{
break;
}
}
return files.GetRange(0, Math.Min(files.Count, maxCount));
}
{
break;
}
}
return files.GetRange(0, Math.Min(files.Count, maxCount));
}
///
/// 預設查詢20個檔案
///
///
///
///
public static IList FindFiles(string directory, string filePattern)
{
int maxCount = 20;
return FindFiles(directory, filePattern, maxCount);
}
/// 預設查詢20個檔案
///
///
///
///
public static IList
{
int maxCount = 20;
return FindFiles(directory, filePattern, maxCount);
}
///
/// 獲取檔案
///
///
///
///
public static IList FindFiles(string directory, string filePattern, int maxCount)
{
List matchedFiles = new List();
IList fileInfos = FindAllFiles(directory);
/// 獲取檔案
///
///
///
///
public static IList
{
List
IList
if (string.IsNullOrWhiteSpace(filePattern))
{
return matchedFiles;
}
{
return matchedFiles;
}
if (maxCount < 0 || maxCount > fileInfos.Count)
{
maxCount = fileInfos.Count;
}
{
maxCount = fileInfos.Count;
}
maxCount--;
foreach (var fileInfo in fileInfos)
{
if (maxCount <0)
{
break;
}
{
if (maxCount <0)
{
break;
}
if (FileHelper.IsMatched(fileInfo.Name, filePattern))
{
matchedFiles.Add(fileInfo);
}
{
matchedFiles.Add(fileInfo);
}
maxCount--;
}
}
return matchedFiles;
}
}
///
///
///
///
///
///
public static IList FindAllFiles(string directory)
{
IList fileInfos = new List();
if (string.IsNullOrWhiteSpace(directory))
{
return fileInfos;
}
///
///
///
///
///
public static IList
{
IList
if (string.IsNullOrWhiteSpace(directory))
{
return fileInfos;
}
if (!Directory.Exists(directory))
{
return fileInfos;
}
{
return fileInfos;
}
DirectoryInfo dir = new DirectoryInfo(directory);
fileInfos = dir.GetFiles();
fileInfos = dir.GetFiles();
return fileInfos;
}
}
///
/// 單個檔案移動
///
///
///
///
public static bool MoveFile(string sourcePath, string targetDirectory)
{
if (!File.Exists(sourcePath))
{
return false;
}
/// 單個檔案移動
///
///
///
///
public static bool MoveFile(string sourcePath, string targetDirectory)
{
if (!File.Exists(sourcePath))
{
return false;
}
if (!Directory.Exists(targetDirectory))
{
return false;
}
{
return false;
}
var targetPath = string.Format("{0}\\{1}", targetDirectory, FileHelper.GetFileName(sourcePath));
while (File.Exists(targetPath))
{
targetPath = FileHelper.Rename(targetPath);
}
{
targetPath = FileHelper.Rename(targetPath);
}
if (sourcePath.Length > 260 || targetPath.Length > 260)
{
return MoveLongPathFile(@""+ targetPath);
}
{
return MoveLongPathFile(@""+ 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;
}
/// 單個檔案移動
///
///
///
///
///
public static bool MoveFile(string fileName, string sourceDirectory, string targetDirectory)
{
if (!File.Exists(fileName))
{
return false;
}
if (!Directory.Exists(sourceDirectory))
{
return false;
}
{
return false;
}
if (!Directory.Exists(targetDirectory))
{
return false;
}
{
return false;
}
string filePath = fileName.Replace(sourceDirectory, targetDirectory);
string fileDir=Path.GetDirectoryName(filePath);
if(!DirectoryHelper.CreateDirectory(fileDir))
{
return false;
}
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;
}
/// 重新生成新的檔案路徑
///
///
///
public static string Rename(string filePath)
{
if (string.IsNullOrWhiteSpace(filePath))
{
return string.Empty;
}
string lastFileName = Path.GetFileNameWithoutExtension(filePath);
string lastFileExtension = Path.GetExtension(filePath);
string lastFileExtension = Path.GetExtension(filePath);
//重新命名,則隨機在原來檔名後面加幾個隨機數字進行組裝成新的名字
Random random = new Random(System.DateTime.Now.Millisecond);
Random random = new Random(System.DateTime.Now.Millisecond);
string randomData = random.Next().ToString();
//把原檔名的名字加上隨機數,組裝成新的檔名(避免重名)
string newFileName = lastFileName + randomData;
return Path.GetDirectoryName(filePath) + "\\" + newFileName + lastFileExtension;
}
string newFileName = lastFileName + randomData;
return Path.GetDirectoryName(filePath) + "\\" + newFileName + lastFileExtension;
}
}
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28699126/viewspace-773038/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- QT串列埠助手(五):檔案操作QT串列埠
- Yoink for Mac(臨時檔案拖放助手)Mac
- 企業雲盤--檔案高效管理助手
- Yoink for Mac(臨時檔案儲存助手)Mac
- Mac臨時檔案儲存助手:YoinkMac
- Java —— 檔案類(File 類)Java
- 陣列助手類(ArrayHelper)陣列
- 類檔案結構_class類檔案的的結構
- MacDroid for mac(安卓裝置檔案傳輸助手)Mac安卓
- Yii2-助手類(Html)HTML
- Yii2-助手類(ArrayHelper)
- Yii2-助手類(formatter)ORM
- dedecms主要類檔案
- 檔案操作類FileUtil
- MacDroid for Mac,強大的安卓檔案傳輸助手Mac安卓
- Yii2-助手類(StringHelper)
- Java : File 檔案類操作Java
- Class類檔案結構
- list集合、txt檔案對比的工具類和檔案讀寫工具類
- Java™ 教程(管理原始檔和類檔案)Java
- Java的特殊類用法:檔案類、內部類、本地類、匿名類Java
- 【JVM】深入解析class類檔案JVM
- java 檔案處理 工具類Java
- C# 檔案IO常用類C#
- c++ IO類,檔案操作C++
- JAVA Class類檔案結構Java
- 一個CSV檔案解析類
- JFileChooser:ExampleFileFilter.java 類檔案FilterJava
- vbs類生成xml檔案(轉)XML
- 什麼是檔案包含漏洞?檔案包含漏洞分類!
- 【/proc/檔案淺析】另類辦法恢復資料檔案和控制檔案
- 垃圾分類助手使用者協議協議
- 用php生成HTML檔案的類PHPHTML
- ServletFileUpload類上傳檔案Servlet
- JAVA類檔案操作和異常Java
- mach-o 檔案分析(解析類)Mac
- 深入理解JVM類檔案格式JVM
- Java壓縮檔案生成工具類Java