添加项目文件。
This commit is contained in:
281
Common/FileHelper.cs
Normal file
281
Common/FileHelper.cs
Normal file
@ -0,0 +1,281 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Web;
|
||||
|
||||
|
||||
namespace Epost.Common
|
||||
{
|
||||
#region 文件
|
||||
/// <summary>
|
||||
/// 文件
|
||||
/// </summary>
|
||||
public static class FileHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 编码方式
|
||||
/// </summary>
|
||||
private static readonly Encoding Encoding = Encoding.UTF8;
|
||||
|
||||
#region 获取文件后缀名
|
||||
/// <summary>
|
||||
/// 获取文件后缀名
|
||||
/// </summary>
|
||||
/// <param name="filePath"></param>
|
||||
/// <param name="isPoint"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetFileExt(string filePath, bool isPoint = false)
|
||||
{
|
||||
if (string.IsNullOrEmpty(filePath))
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
if (filePath.LastIndexOf(".", StringComparison.Ordinal) > 0)
|
||||
{
|
||||
if (isPoint)
|
||||
{
|
||||
return filePath.Substring(filePath.LastIndexOf(".", StringComparison.Ordinal)); //文件扩展名,含“.”
|
||||
}
|
||||
else
|
||||
{
|
||||
return filePath.Substring(filePath.LastIndexOf(".", StringComparison.Ordinal) + 1); //文件扩展名,不含“.”
|
||||
}
|
||||
}
|
||||
return string.Empty;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 获取文件名
|
||||
|
||||
/// <summary>
|
||||
/// 获取文件名
|
||||
/// </summary>
|
||||
/// <param name="filePath"></param>
|
||||
/// <param name="isExt"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetFileName(string filePath, bool isExt = true)
|
||||
{
|
||||
if (string.IsNullOrEmpty(filePath))
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
var fileName = filePath.Substring(filePath.LastIndexOf(@"\", StringComparison.Ordinal) + 1);
|
||||
if (isExt)
|
||||
{
|
||||
return fileName;
|
||||
}
|
||||
fileName = fileName.Substring(0, fileName.LastIndexOf(".", StringComparison.Ordinal));
|
||||
return fileName;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 获得当前绝对路径
|
||||
/// <summary>
|
||||
/// 获得当前绝对路径
|
||||
/// </summary>
|
||||
/// <param name="strPath">指定的路径</param>
|
||||
/// <returns>绝对路径</returns>
|
||||
public static string GeAbsolutePath(string strPath)
|
||||
{
|
||||
if (strPath.ToLower().StartsWith("http://"))
|
||||
{
|
||||
return strPath;
|
||||
}
|
||||
if (HttpContext.Current != null)
|
||||
{
|
||||
return HttpContext.Current.Server.MapPath(strPath);
|
||||
}
|
||||
else //非web程序引用
|
||||
{
|
||||
strPath = strPath.Replace("/", "\\");
|
||||
if (strPath.StartsWith("\\"))
|
||||
{
|
||||
strPath = strPath.Substring(strPath.IndexOf('\\', 1)).TrimStart('\\');
|
||||
}
|
||||
return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 获得当前虚拟路径
|
||||
/// <summary>
|
||||
/// 获得当前虚拟路径
|
||||
/// </summary>
|
||||
/// <param name="strPath">指定的路径</param>
|
||||
/// <returns>绝对路径</returns>
|
||||
public static string GeVirtualPath(string strPath)
|
||||
{
|
||||
if (strPath.ToLower().StartsWith("http://"))
|
||||
{
|
||||
return strPath;
|
||||
}
|
||||
if (HttpContext.Current != null)
|
||||
{
|
||||
return HttpContext.Current.Request.ApplicationPath + strPath;
|
||||
}
|
||||
else //非web程序引用
|
||||
{
|
||||
strPath = strPath.Replace("/", "\\");
|
||||
if (strPath.StartsWith("\\"))
|
||||
{
|
||||
strPath = strPath.Substring(strPath.IndexOf('\\', 1)).TrimStart('\\');
|
||||
}
|
||||
return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 创建文件夹
|
||||
/// <summary>
|
||||
/// 创建文件夹
|
||||
/// </summary>
|
||||
/// <param name="path">文件夹路径</param>
|
||||
/// <returns>返回传来的路径</returns>
|
||||
public static bool CreateFile(string path)
|
||||
{
|
||||
if (!Directory.Exists(path)) //检查文件是否存在
|
||||
{
|
||||
Directory.CreateDirectory(path); //创建文件
|
||||
}
|
||||
return Directory.Exists(path);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 判断文件是否存在
|
||||
/// <summary>
|
||||
/// 判断文件是否存在
|
||||
/// </summary>
|
||||
/// <param name="path"></param>
|
||||
/// <returns></returns>
|
||||
public static bool FileExists(string path)
|
||||
{
|
||||
return File.Exists(GeAbsolutePath(path));
|
||||
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 复制文件夹
|
||||
|
||||
/// <summary>
|
||||
/// 复制文件夹
|
||||
/// </summary>
|
||||
/// <param name="sourceDir"></param>
|
||||
/// <param name="targetDir"></param>
|
||||
/// <param name="notInclude"></param>
|
||||
/// <returns></returns>
|
||||
public static bool CopyDirectory(string sourceDir, string targetDir, List<string> notInclude = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!Directory.Exists(sourceDir))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (!Directory.Exists(targetDir)) //检查文件是否存在
|
||||
{
|
||||
Directory.CreateDirectory(targetDir); //创建文件
|
||||
File.SetAttributes(targetDir, File.GetAttributes(sourceDir));
|
||||
}
|
||||
if (targetDir[targetDir.Length - 1] != Path.DirectorySeparatorChar)
|
||||
targetDir = targetDir + Path.DirectorySeparatorChar;
|
||||
|
||||
string[] files = Directory.GetFiles(sourceDir);
|
||||
foreach (string file in files)
|
||||
{
|
||||
if (notInclude != null && notInclude.Contains(file.ToLower()))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
File.Copy(file, targetDir + Path.GetFileName(file), true);
|
||||
File.SetAttributes(targetDir + Path.GetFileName(file), FileAttributes.Normal);
|
||||
}
|
||||
|
||||
string[] dirs = Directory.GetDirectories(sourceDir);
|
||||
foreach (string dir in dirs)
|
||||
{
|
||||
if (notInclude != null && notInclude.Contains(dir.ToLower()))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (!CopyDirectory(dir, targetDir + Path.GetFileName(dir), notInclude))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// new LogHelper(typeof(FileHelper)).Error(ex);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 写入文件
|
||||
/// <summary>
|
||||
/// 写入文件
|
||||
/// </summary>
|
||||
/// <param name="filePath">文件名</param>
|
||||
/// <param name="content">文件内容</param>
|
||||
public static bool WriteFile(string filePath, string content)
|
||||
{
|
||||
try
|
||||
{
|
||||
var fs = new FileStream(filePath, FileMode.Create);
|
||||
Encoding encode = Encoding;
|
||||
//获得字节数组
|
||||
byte[] data = encode.GetBytes(content);
|
||||
//开始写入
|
||||
fs.Write(data, 0, data.Length);
|
||||
//清空缓冲区、关闭流
|
||||
fs.Flush();
|
||||
fs.Close();
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// new LogHelper(typeof(FileHelper)).Error(ex);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 读取文件
|
||||
/// <summary>
|
||||
/// 读取文件
|
||||
/// </summary>
|
||||
/// <param name="filePath"></param>
|
||||
/// <returns></returns>
|
||||
public static string ReadFile(string filePath)
|
||||
{
|
||||
return ReadFile(filePath, Encoding);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 读取文件
|
||||
/// </summary>
|
||||
/// <param name="filePath"></param>
|
||||
/// <param name="encoding"></param>
|
||||
/// <returns></returns>
|
||||
public static string ReadFile(string filePath, Encoding encoding)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (var sr = new StreamReader(filePath, encoding))
|
||||
{
|
||||
return sr.ReadToEnd();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// new LogHelper(typeof(FileHelper)).Error(ex);
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
#endregion
|
||||
}
|
Reference in New Issue
Block a user