发布:2023/12/28 20:17:57作者:管理员 来源:本站 浏览次数:502
一般使用“GetAllFiles”,“DeleteFiles”方法。可以自行决定用哪一个方法
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace DataManageBLL
{
public class FileHelper
{
///// <summary>
///// 遍历获取文件夹下所有文件
///// </summary>
///// <param name="fileFolder">要搜索的文件夹</param>
///// <param name="allfiles">返回的文件列表</param>
//public static void GetAllFiles(string fileFolder, ref List<string> allfiles)
//{
// DirectoryInfo Dir = new DirectoryInfo(fileFolder);
// FileInfo[] files = Dir.GetFiles();
// foreach (var fileitem in files)
// {
// allfiles.Add(fileitem.FullName);
// }
// DirectoryInfo[] directorys = Dir.GetDirectories();
// foreach (var directoryitem in directorys)
// {
// GetAllFiles(directoryitem.FullName, ref allfiles);
// }
//}
/// <summary>
/// 遍历获取文件夹下所有文件
/// </summary>
/// <param name="fileFolder">要搜索的文件夹</param>
/// <param name="allfiles">返回的文件列表</param>
public static void GetAllFiles(string fileFolder, ref List<string> allfiles, string ext = null, DateTime? startTime = null)
{
DateTime lastUploadDateTime = DateTime.MinValue;
GetAllFiles(fileFolder, ref allfiles, ref lastUploadDateTime, ext, startTime);
}
public static void GetAllFilesByImgPath(string decPath, ref List<string> allfiles, ref List<DateTime> lastUploadTimeList, string ext = null, DateTime? startTime = null)
{
DirectoryInfo Dir = new DirectoryInfo(decPath);
DirectoryInfo[] files = Dir.GetDirectories();
if (files != null && files.Count() > 0)
{
if (!string.IsNullOrEmpty(ext))
{
Regex regex = new Regex(ext);
files = files.Where(x => x.LastWriteTime > startTime).Where(x => regex.IsMatch(x.Name)).OrderBy(x => x.LastWriteTime).ToArray();
}
else
files = files.Where(x => x.LastWriteTime > startTime).OrderBy(x => x.LastWriteTime).ToArray();
if (startTime.HasValue)
{
foreach (var item in files)
{
allfiles.Add(item.FullName);
lastUploadTimeList.Add(item.LastWriteTime);
}
}
}
}
/// <summary>
/// 遍历获取文件夹下所有文件
/// </summary>
/// <param name="fileFolder">要搜索的文件夹</param>
/// <param name="allfiles">返回的文件列表</param>
public static void GetAllFiles(string fileFolder, ref List<string> allfiles, ref DateTime lastUploadTime, string ext = null, DateTime? startTime = null)
{
DirectoryInfo Dir = new DirectoryInfo(fileFolder);
FileInfo[] files = new FileInfo[] { };
if (Dir.Exists)
files = Dir.GetFiles();
foreach (var fileitem in files)
{
if (startTime == null || (startTime != null && fileitem.LastWriteTime < startTime.Value))
{
if (string.IsNullOrEmpty(ext))
{
allfiles.Add(fileitem.FullName);
if (lastUploadTime < fileitem.LastWriteTime)
{
lastUploadTime = fileitem.LastWriteTime;
}
}
else
{
string fileExt = Path.GetExtension(fileitem.FullName);
if (fileExt.ToLower() == ext.ToLower())
{
allfiles.Add(fileitem.FullName);
if (lastUploadTime < fileitem.LastWriteTime)
{
lastUploadTime = fileitem.LastWriteTime;
}
}
}
}
}
DirectoryInfo[] directorys = Dir.GetDirectories();
foreach (var directoryitem in directorys)
{
//if (startTime == null || (startTime != null && directoryitem.LastWriteTime < startTime.Value))
GetAllFiles(directoryitem.FullName, ref allfiles, ref lastUploadTime, ext, startTime);
}
}
/// <summary>
/// 遍历获取文件夹下所有文件
/// </summary>
/// <param name="fileFolder">要搜索的文件夹</param>
/// <param name="allfiles">返回的文件列表</param>
public static void DeleteFiles(string fileFolder, ref List<string> allfiles, string ext = null, DateTime? startTime = null)
{
DateTime lastUploadDateTime = DateTime.MinValue;
DeleteFilesDetail(fileFolder, ref allfiles, ref lastUploadDateTime, ext, startTime);
}
/// <summary>
/// 遍历获取文件夹下所有文件
/// </summary>
/// <param name="fileFolder">要搜索的文件夹</param>
/// <param name="allfiles">返回的文件列表</param>
public static void DeleteFilesDetail(string fileFolder, ref List<string> allfiles, ref DateTime lastUploadTime, string ext = null, DateTime? startTime = null)
{
DirectoryInfo Dir = new DirectoryInfo(fileFolder);
FileInfo[] files = new FileInfo[] { };
if (Dir.Exists)
files = Dir.GetFiles();
foreach (var fileitem in files)
{
if (startTime == null || (startTime != null && fileitem.LastWriteTime < startTime.Value))
{
try
{
File.Delete(fileitem.FullName);
}
catch (Exception ex)
{
continue;
}
}
}
DirectoryInfo[] directorys = Dir.GetDirectories();
foreach (var directoryitem in directorys)
{
//if (startTime == null || (startTime != null && directoryitem.LastWriteTime < startTime.Value))
DeleteFilesDetail(directoryitem.FullName, ref allfiles, ref lastUploadTime, ext, startTime);
}
}
public static void GetAllFilesByPath(string decPath, ref List<string> allfiles, ref List<DateTime> lastUploadTimeList, string ext = null, DateTime? startTime = null)
{
DirectoryInfo Dir = new DirectoryInfo(decPath);
FileInfo[] files = Dir.GetFiles();
if (files != null && files.Count() > 0)
{
files = files.Where(x => x.LastWriteTime > startTime).ToArray();
if (startTime.HasValue)
{
foreach (var item in files)
{
if (!string.IsNullOrEmpty(ext))
{
if (item.Extension.ToLower() == ext.ToLower())
{
allfiles.Add(item.FullName);
lastUploadTimeList.Add(item.LastWriteTime);
}
}
else if (string.IsNullOrEmpty(item.Extension))
{
allfiles.Add(item.FullName);
lastUploadTimeList.Add(item.LastWriteTime);
}
}
}
}
DirectoryInfo[] fileDireS = Dir.GetDirectories();
if (fileDireS != null && fileDireS.Count() > 0)
{
foreach (var item in fileDireS)
{
GetAllFilesByPath(item.FullName, ref allfiles, ref lastUploadTimeList, ext, startTime);
}
}
}
/// <summary>
/// 根据文件路径获取文件字节流
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
public static byte[] GetFileToBytes(string filePath)
{
System.IO.Stream sm = GetFile(filePath);
byte[] buffer;
if (sm != null)
{
buffer = new byte[sm.Length];
sm.Read(buffer, 0, (int)sm.Length);
sm.Close();
return buffer;
}
return null;
}
/// <summary>
/// 根据文件路径获取文件流
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
public static Stream GetFile(string filePath)
{
if (File.Exists(filePath))
{
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
return fs;
}
return null;
}
/// <summary>
/// 获取目录下的所有子目录
/// </summary>
/// <param name="fileFolder"></param>
/// <returns></returns>
public static List<string> GetAllDirectorys(string fileFolder, DateTime? startTime = null)
{
if (!Directory.Exists(fileFolder))
{
//路径不存在
return null;
}
DirectoryInfo Dir = new DirectoryInfo(fileFolder);
if (startTime == null)
return Dir.GetDirectories().Select(i => i.FullName).ToList();
else
return Dir.GetDirectories().Where(i => i.CreationTime <= startTime).Select(i => i.FullName).ToList();
}
/// <summary>
/// 获取文件的MD5哈希值
/// </summary>
/// <param name="filePath">文件名</param>
/// <returns></returns>
public static string GetFileMD5Hash(string filePath)
{
FileStream get_file = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
MD5CryptoServiceProvider get_md5 = new MD5CryptoServiceProvider();
byte[] hash_byte = get_md5.ComputeHash(get_file);
get_file.Close();
string result = BitConverter.ToString(hash_byte);
return result.Replace("-", "");
}
/// <summary>
/// 获取某个流的MD5值
/// 注:流必须先seek(0,begin)
/// </summary>
/// <param name="stream"></param>
/// <returns></returns>
public static string GetStreamMD5Hash(Stream stream)
{
MD5CryptoServiceProvider get_md5 = new MD5CryptoServiceProvider();
byte[] hash_byte = get_md5.ComputeHash(stream);
string result = BitConverter.ToString(hash_byte);
return result.Replace("-", "");
}
/// <summary>
/// 删除文件
/// </summary>
/// <param name="filePath"></param>
public static void FileDelete(string filePath)
{
if (File.Exists(filePath))
{
File.Delete(filePath);
}
}
/// <summary>
/// 移动文件(多个)
/// </summary>
/// <param name="srcFileFullPathes">源文件(数组)</param>
/// <param name="destFileSubPathes">目标文件(数组)</param>
/// <param name="isOverwrite">是否覆盖</param>
/// <returns>
/// 拷贝后文件的全路径
/// </returns>
public static void Move(string[] srcFileFullPathes, string[] destFileFullPathes, bool isOverwrite)
{
//参数检查
if (srcFileFullPathes.Length != destFileFullPathes.Length)
{
throw new Exception("[源文件个数]与[目标文件个数]不匹配!");
}
//检查目标路径是否存在,不存在就创建
for (int i = 0; i < destFileFullPathes.Length; i++)
{
//路径相同不检查目录
if (string.Compare(srcFileFullPathes[i], destFileFullPathes[i]) == 0)
{
continue;
}
//检查目标文件目录
CheckDirectory(destFileFullPathes[i]);
}
try
{
for (int i = 0; i < srcFileFullPathes.Length; i++)
{
//路径相同不移动
if (string.Compare(srcFileFullPathes[i], destFileFullPathes[i]) == 0) continue;
if (!File.Exists(destFileFullPathes[i]))
{
//移动文件
File.Move(srcFileFullPathes[i], destFileFullPathes[i]);
}
else if (isOverwrite)
{ //覆盖
File.Delete(destFileFullPathes[i]);
File.Move(srcFileFullPathes[i], destFileFullPathes[i]);
}
}
}
catch (Exception ex)
{
throw;
}
}
/// <summary>
/// 检查目录是否存在,不存在就创建目录
/// </summary>
/// <param name="filePath"></param>
public static void CheckDirectory(string filePath)
{
string directory = filePath.Substring(0, filePath.LastIndexOf(@"\"));
if (!Directory.Exists(directory))
{
try
{
Directory.CreateDirectory(directory);
}
catch (Exception ex)
{
throw;
}
}
}
/// <summary>
/// 将A文件夹里面的文件和文件夹移动到B文件夹
/// </summary>
/// <param name="sourcedirectory"></param>
/// <param name="destinationdirectory"></param>
public static void FolderMoveToNewFolder(string sourcedirectory, string destinationdirectory)
{
try
{
DirectoryInfo nowFolder = new DirectoryInfo(sourcedirectory);
destinationdirectory = Path.Combine(destinationdirectory, nowFolder.Name);
if (!Directory.Exists(destinationdirectory))
Directory.CreateDirectory(destinationdirectory);
string[] fileList = Directory.GetFileSystemEntries(sourcedirectory);
foreach (string file in fileList)
{
if (Directory.Exists(destinationdirectory))
{
//Directory.Move(file, destinationdirectory);
DirectoryInfo folder = new DirectoryInfo(file);
string strCreateFileName = destinationdirectory + "\\" + folder.Name;
if (!Directory.Exists(strCreateFileName))
folder.MoveTo(strCreateFileName);
else
folder.Delete();
}
else
Directory.Move(sourcedirectory, destinationdirectory);
if (File.Exists(file))
{
File.Move(file, destinationdirectory);
//FileInfo fi = new FileInfo(file);
//fi.MoveTo(newFolderPath);
}
}
}
catch (Exception ex)
{
}
}
}
}
© Copyright 2014 - 2024 柏港建站平台 ejk5.com. 渝ICP备16000791号-4