using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Linq;
namespace Mini.Common
{
public class FileUnit
{
///
/// 连接基路径和子路径,比如把 c: 与 test.doc 连接成 c:\test.doc
///
/// 基路径,范例:c:
/// 子路径,可以是文件名, 范例:test.doc
public static string JoinPath(string basePath, string subPath)
{
basePath = basePath.TrimEnd('/').TrimEnd('\\');
subPath = subPath.TrimStart('/').TrimStart('\\');
string path = basePath + "\\" + subPath;
return path.Replace("/", "\\").ToLower();
}
///
/// 获取系统物理路径
///
///
public static string GetBaseDirectory()
{
string path = AppDomain.CurrentDomain.BaseDirectory;
return path;
}
///
/// 新建文件夹并写入文件
///
/// 文件夹相对路径(/文件夹/)
/// 文件名(abc.txt)
/// 内容
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);
}
///
/// 新建文件夹并写入文件
///
/// 文件夹相对路径(/文件夹/)
/// 文件名
///
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();
}
///
/// 将字符串写入文件,文件不存在则创建
///
/// 文件的绝对路径
/// 数据
public static void Write(string filePath, string content)
{
Write(filePath, StringToBytes(content));
}
///
/// 将字符串写入文件,文件不存在则创建
///
/// 文件的绝对路径
/// 数据
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);
}
///
/// 字符串转换成字节数组
///
/// 数据,默认字符编码utf-8
public static byte[] StringToBytes(string data)
{
return StringToBytes(data, Encoding.GetEncoding("utf-8"));
}
///
/// 字符串转换成字节数组
///
/// 数据
/// 字符编码
public static byte[] StringToBytes(string data, Encoding encoding)
{
if (string.IsNullOrWhiteSpace(data))
return new byte[] { };
return encoding.GetBytes(data);
}
///
/// 删除文件
///
/// 文件集合的绝对路径
public static void Delete(IEnumerable filePaths)
{
foreach (var filePath in filePaths)
Delete(filePath);
}
///
/// 删除文件
///
/// 文件的绝对路径
public static void Delete(string filePath)
{
if (string.IsNullOrWhiteSpace(filePath))
return;
System.IO.File.Delete(filePath);
}
///
/// 删除文件和文件夹
///
///
public static void DeleteDirectory(IEnumerable filePaths)
{
foreach (var filePath in filePaths)
{
DeleteDirectory(filePath);
}
}
///
/// 删除文件夹和文件
///
///
private static void DeleteDirectory(string filePath)
{
if (string.IsNullOrWhiteSpace(filePath))
return;
var listpath = GetAllFiles(filePath);
Delete(listpath);
Directory.Delete(filePath);
}
///
/// 获取目录中全部文件列表,包括子目录
///
/// 目录绝对路径
public static List GetAllFiles(string directoryPath)
{
return Directory.GetFiles(directoryPath, "*.*", SearchOption.AllDirectories).ToList();
}
///
/// 获取文件目录
///
///
///
public static List GetAllDirectory(string directoryPath)
{
return Directory.GetDirectories(directoryPath, "*", SearchOption.TopDirectoryOnly).ToList();
}
#region Read(读取文件到字符串)
///
/// 读取文件到字符串
///
/// 文件的绝对路径
public static string Read(string filePath)
{
return Read(filePath, Encoding.GetEncoding("utf-8"));
}
///
/// 读取文件到字符串
///
/// 文件的绝对路径
/// 字符编码
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(将文件读取到字节流中)
///
/// 将文件读取到字节流中
///
/// 文件的绝对路径
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
///
/// 获取文件MD5值
///
/// 文件绝对路径
/// MD5值
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);
}
}
}
}