284 lines
9.9 KiB
C#
284 lines
9.9 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using System.Text;
|
||
using System.Linq;
|
||
namespace Mini.Common
|
||
{
|
||
public class FileUnit
|
||
{
|
||
/// <summary>
|
||
/// 连接基路径和子路径,比如把 c: 与 test.doc 连接成 c:\test.doc
|
||
/// </summary>
|
||
/// <param name="basePath">基路径,范例:c:</param>
|
||
/// <param name="subPath">子路径,可以是文件名, 范例:test.doc</param>
|
||
public static string JoinPath(string basePath, string subPath)
|
||
{
|
||
basePath = basePath.TrimEnd('/').TrimEnd('\\');
|
||
subPath = subPath.TrimStart('/').TrimStart('\\');
|
||
string path = basePath + "\\" + subPath;
|
||
return path.Replace("/", "\\").ToLower();
|
||
}
|
||
/// <summary>
|
||
/// 获取系统物理路径
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public static string GetBaseDirectory()
|
||
{
|
||
string path = AppDomain.CurrentDomain.BaseDirectory;
|
||
return path;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 新建文件夹并写入文件
|
||
/// </summary>
|
||
/// <param name="folderPath">文件夹相对路径(/文件夹/)</param>
|
||
/// <param name="fileName">文件名(abc.txt)</param>
|
||
/// <param name="content">内容</param>
|
||
public static void WriteWithDirectory(string folderPath, string fileName, string content)
|
||
{
|
||
// string absoluteFlolder = GetBaseDirectory() + folderPath;
|
||
if (!Directory.Exists(folderPath))//判断文件夹是否存在
|
||
{
|
||
Directory.CreateDirectory(folderPath);//不存在则创建文件夹
|
||
}
|
||
if (string.IsNullOrEmpty(fileName))
|
||
{
|
||
fileName = string.Format("{0:yyMMddHHmmss}.txt", DateTime.Now);
|
||
}
|
||
string absolutefile = Path.Combine(folderPath, fileName);
|
||
Write(absolutefile, content);
|
||
}
|
||
|
||
public static void AppendFile(string folderPath, string fileName, string content)
|
||
{
|
||
// string absoluteFlolder = GetBaseDirectory() + folderPath;
|
||
if (!Directory.Exists(folderPath))//判断文件夹是否存在
|
||
{
|
||
Directory.CreateDirectory(folderPath);//不存在则创建文件夹
|
||
}
|
||
if (string.IsNullOrEmpty(fileName))
|
||
{
|
||
fileName = string.Format("{0:yyMMddHHmmss}.txt", DateTime.Now);
|
||
}
|
||
string absolutefile = Path.Combine(folderPath, fileName);
|
||
WriteLine(absolutefile, content);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 新建文件夹并写入文件
|
||
/// </summary>
|
||
/// <param name="folderPath">文件夹相对路径(/文件夹/)</param>
|
||
/// <param name="fileName">文件名</param>
|
||
/// <param name="stream"></param>
|
||
public static void WriteWithDirectory(string folderPath, string fileName, Stream stream)
|
||
{
|
||
// string absoluteFlolder = GetBaseDirectory() + folderPath;
|
||
if (!Directory.Exists(folderPath))//判断文件夹是否存在
|
||
{
|
||
Directory.CreateDirectory(folderPath);//不存在则创建文件夹
|
||
}
|
||
string absolutefile = folderPath + fileName;
|
||
byte[] buffer = new byte[stream.Length];
|
||
stream.Read(buffer, 0, buffer.Length); //将流的内容读到缓冲区
|
||
FileStream fs = new FileStream(absolutefile, FileMode.OpenOrCreate, FileAccess.Write);
|
||
fs.Write(buffer, 0, buffer.Length);
|
||
fs.Flush();
|
||
fs.Close();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 将字符串写入文件,文件不存在则创建
|
||
/// </summary>
|
||
/// <param name="filePath">文件的绝对路径</param>
|
||
/// <param name="content">数据</param>
|
||
public static void Write(string filePath, string content)
|
||
{
|
||
Write(filePath, StringToBytes(content));
|
||
}
|
||
|
||
/// <summary>
|
||
/// 将字符串写入文件,文件不存在则创建
|
||
/// </summary>
|
||
/// <param name="filePath">文件的绝对路径</param>
|
||
/// <param name="content">数据</param>
|
||
public static void Write(string filePath, byte[] bytes)
|
||
{
|
||
if (string.IsNullOrWhiteSpace(filePath))
|
||
return;
|
||
if (bytes == null)
|
||
return;
|
||
System.IO.File.WriteAllBytes(filePath, bytes);
|
||
}
|
||
|
||
public static void WriteLine(string filePath, string contents)
|
||
{
|
||
if (string.IsNullOrWhiteSpace(filePath))
|
||
return;
|
||
if (contents == null)
|
||
return;
|
||
System.IO.File.AppendAllText(filePath, contents);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 字符串转换成字节数组
|
||
/// </summary>
|
||
/// <param name="data">数据,默认字符编码utf-8</param>
|
||
public static byte[] StringToBytes(string data)
|
||
{
|
||
return StringToBytes(data, Encoding.GetEncoding("utf-8"));
|
||
}
|
||
/// <summary>
|
||
/// 字符串转换成字节数组
|
||
/// </summary>
|
||
/// <param name="data">数据</param>
|
||
/// <param name="encoding">字符编码</param>
|
||
public static byte[] StringToBytes(string data, Encoding encoding)
|
||
{
|
||
if (string.IsNullOrWhiteSpace(data))
|
||
return new byte[] { };
|
||
return encoding.GetBytes(data);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 删除文件
|
||
/// </summary>
|
||
/// <param name="filePaths">文件集合的绝对路径</param>
|
||
public static void Delete(IEnumerable<string> filePaths)
|
||
{
|
||
foreach (var filePath in filePaths)
|
||
Delete(filePath);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 删除文件
|
||
/// </summary>
|
||
/// <param name="filePath">文件的绝对路径</param>
|
||
public static void Delete(string filePath)
|
||
{
|
||
if (string.IsNullOrWhiteSpace(filePath))
|
||
return;
|
||
System.IO.File.Delete(filePath);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 删除文件和文件夹
|
||
/// </summary>
|
||
/// <param name="filePath"></param>
|
||
public static void DeleteDirectory(IEnumerable<string> filePaths)
|
||
{
|
||
foreach (var filePath in filePaths)
|
||
{
|
||
DeleteDirectory(filePath);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 删除文件夹和文件
|
||
/// </summary>
|
||
/// <param name="filePath"></param>
|
||
private static void DeleteDirectory(string filePath)
|
||
{
|
||
if (string.IsNullOrWhiteSpace(filePath))
|
||
return;
|
||
var listpath = GetAllFiles(filePath);
|
||
Delete(listpath);
|
||
Directory.Delete(filePath);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取目录中全部文件列表,包括子目录
|
||
/// </summary>
|
||
/// <param name="directoryPath">目录绝对路径</param>
|
||
public static List<string> GetAllFiles(string directoryPath)
|
||
{
|
||
return Directory.GetFiles(directoryPath, "*.*", SearchOption.AllDirectories).ToList();
|
||
}
|
||
/// <summary>
|
||
/// 获取文件目录
|
||
/// </summary>
|
||
/// <param name="directoryPath"></param>
|
||
/// <returns></returns>
|
||
public static List<string> GetAllDirectory(string directoryPath)
|
||
{
|
||
return Directory.GetDirectories(directoryPath, "*", SearchOption.TopDirectoryOnly).ToList();
|
||
}
|
||
#region Read(读取文件到字符串)
|
||
|
||
/// <summary>
|
||
/// 读取文件到字符串
|
||
/// </summary>
|
||
/// <param name="filePath">文件的绝对路径</param>
|
||
public static string Read(string filePath)
|
||
{
|
||
return Read(filePath, Encoding.GetEncoding("utf-8"));
|
||
}
|
||
|
||
/// <summary>
|
||
/// 读取文件到字符串
|
||
/// </summary>
|
||
/// <param name="filePath">文件的绝对路径</param>
|
||
/// <param name="encoding">字符编码</param>
|
||
public static string Read(string filePath, Encoding encoding)
|
||
{
|
||
if (!System.IO.File.Exists(filePath))
|
||
return string.Empty;
|
||
using (var reader = new StreamReader(filePath, encoding))
|
||
{
|
||
return reader.ReadToEnd();
|
||
}
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region ReadToBytes(将文件读取到字节流中)
|
||
|
||
/// <summary>
|
||
/// 将文件读取到字节流中
|
||
/// </summary>
|
||
/// <param name="filePath">文件的绝对路径</param>
|
||
public static byte[] ReadToBytes(string filePath)
|
||
{
|
||
if (!System.IO.File.Exists(filePath))
|
||
return null;
|
||
FileInfo fileInfo = new FileInfo(filePath);
|
||
int fileSize = (int)fileInfo.Length;
|
||
using (BinaryReader reader = new BinaryReader(fileInfo.Open(FileMode.Open)))
|
||
{
|
||
return reader.ReadBytes(fileSize);
|
||
}
|
||
}
|
||
|
||
#endregion
|
||
|
||
/// <summary>
|
||
/// 获取文件MD5值
|
||
/// </summary>
|
||
/// <param name="fileName">文件绝对路径</param>
|
||
/// <returns>MD5值</returns>
|
||
public static string GetMD5HashFromFile(string fileName)
|
||
{
|
||
try
|
||
{
|
||
FileStream file = new FileStream(fileName, FileMode.Open);
|
||
System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
|
||
byte[] retVal = md5.ComputeHash(file);
|
||
file.Close();
|
||
|
||
StringBuilder sb = new StringBuilder();
|
||
for (int i = 0; i < retVal.Length; i++)
|
||
{
|
||
sb.Append(retVal[i].ToString("x2"));
|
||
}
|
||
return sb.ToString();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
throw new Exception("GetMD5HashFromFile() fail,error:" + ex.Message);
|
||
}
|
||
}
|
||
|
||
|
||
}
|
||
}
|