252 lines
7.7 KiB
C#
252 lines
7.7 KiB
C#
|
using System;
|
|||
|
using System.Text;
|
|||
|
using System.Configuration;
|
|||
|
using System.Web.UI.WebControls;
|
|||
|
using System.IO;
|
|||
|
using System.Web;
|
|||
|
using System.Net;
|
|||
|
using Newtonsoft.Json;
|
|||
|
using System.Web.UI;
|
|||
|
using System.Web.Hosting;
|
|||
|
|
|||
|
namespace Eson.Utils
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// 文件操作帮助类
|
|||
|
/// </summary>
|
|||
|
public class Assistant
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
///删除指定目录下的所有文件
|
|||
|
/// </summary>
|
|||
|
/// <param name="srcPath">目录</param>
|
|||
|
public static void Deletes(string srcPath)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
string[] fileList = Directory.GetFileSystemEntries(srcPath);//获取目录下所有文件目录和文件
|
|||
|
|
|||
|
foreach (string file in fileList)
|
|||
|
{
|
|||
|
string pt = file;
|
|||
|
//检查是否含有"\\"字符
|
|||
|
if (pt.IndexOf("\\") > -1)
|
|||
|
{
|
|||
|
pt = pt.Replace("\\", "/");//把"\\"替换成"/"
|
|||
|
}
|
|||
|
|
|||
|
//检查是否为目录
|
|||
|
if (Directory.Exists(pt))
|
|||
|
{
|
|||
|
if (pt.IndexOf("App_Data") > -1) //检查目录名是否为"App_Data" 如果是不执行操作
|
|||
|
{ }
|
|||
|
else if (pt.IndexOf("Admin") > -1)//检查目录名是否为"Admin" 如果是不执行操作
|
|||
|
{ }
|
|||
|
else
|
|||
|
{
|
|||
|
//删除该目录及目录下所有文件
|
|||
|
Directory.Delete(pt, true);
|
|||
|
}
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
//删除文件
|
|||
|
File.Delete(pt);
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
}
|
|||
|
catch
|
|||
|
{
|
|||
|
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 在执行完导入操作后,删除导入过程中临时存放的导入文件
|
|||
|
/// </summary>
|
|||
|
/// <param name="importFilePath"></param>
|
|||
|
public static void DeleteFileOfImport(string importFilePath)
|
|||
|
{
|
|||
|
if (Convert.ToBoolean(ConfigurationManager.AppSettings["ImportPathDel"]))
|
|||
|
{
|
|||
|
//删除文件
|
|||
|
try
|
|||
|
{
|
|||
|
System.IO.File.Delete(importFilePath);
|
|||
|
}
|
|||
|
catch { }
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 检查是否存在某文件,存在则删除
|
|||
|
/// </summary>
|
|||
|
/// <param name="strFilePath">文件路径</param>
|
|||
|
public static void DeleteFile(string strFilePath)
|
|||
|
{
|
|||
|
if (File.Exists(strFilePath))
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
File.Delete(strFilePath);
|
|||
|
}
|
|||
|
catch { }
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 上传文件
|
|||
|
/// </summary>
|
|||
|
/// <param name="fuXls">上传控件</param>
|
|||
|
/// <param name="strFilePath">文件路径</param>
|
|||
|
/// <param name="strPath">文件上层文件夹路径</param>
|
|||
|
public static void SaveFile(FileUpload fuXls, out string strFilePath, string strPath)
|
|||
|
{
|
|||
|
string virtualPath = ConfigurationManager.AppSettings[strPath] + fuXls.FileName;
|
|||
|
strFilePath = System.Web.HttpContext.Current.Server.MapPath(virtualPath);
|
|||
|
DeleteFile(strFilePath);
|
|||
|
fuXls.SaveAs(strFilePath);
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 上传文件
|
|||
|
/// </summary>
|
|||
|
/// <param name="fuXls">上传控件</param>
|
|||
|
/// <param name="strPath">文件上层文件夹路径</param>
|
|||
|
/// <param name="saveName">文件名称(默认为空)</param>
|
|||
|
public static string UpLoadFile(FileUpload fuXls, string strPath, string saveName = "")
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
string fileName = fuXls.FileName;
|
|||
|
string extension = System.IO.Path.GetExtension(fileName).ToLower();
|
|||
|
if (string.IsNullOrEmpty(saveName))
|
|||
|
{
|
|||
|
saveName = GetRandomCode(15);
|
|||
|
}
|
|||
|
saveName = saveName + extension;
|
|||
|
string strFilePath = string.Empty;
|
|||
|
string virtualPath = strPath + saveName;
|
|||
|
strFilePath = System.Web.HttpContext.Current.Server.MapPath(virtualPath);
|
|||
|
DeleteFile(strFilePath);
|
|||
|
fuXls.SaveAs(strFilePath);
|
|||
|
return strFilePath;
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
throw ex;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
#region 从字符串里随机得到,规定个数的字符串
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 从字符串里随机得到,规定个数的字符串.
|
|||
|
/// </summary>
|
|||
|
/// <param name="allChar"></param>
|
|||
|
/// <param name="CodeCount"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public static string GetRandomCode(int CodeCount)
|
|||
|
{
|
|||
|
string allChar = "1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,i,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
|
|||
|
string[] allCharArray = allChar.Split(',');
|
|||
|
string RandomCode = "";
|
|||
|
int temp = -1;
|
|||
|
Random rand = new Random();
|
|||
|
for (int i = 0; i < CodeCount; i++)
|
|||
|
{
|
|||
|
if (temp != -1)
|
|||
|
{
|
|||
|
rand = new Random(temp * i * ((int)DateTime.Now.Ticks));
|
|||
|
}
|
|||
|
|
|||
|
int t = rand.Next(allCharArray.Length - 1);
|
|||
|
|
|||
|
while (temp == t)
|
|||
|
{
|
|||
|
t = rand.Next(allCharArray.Length - 1);
|
|||
|
}
|
|||
|
|
|||
|
temp = t;
|
|||
|
RandomCode += allCharArray[t];
|
|||
|
}
|
|||
|
return RandomCode;
|
|||
|
}
|
|||
|
|
|||
|
#endregion
|
|||
|
|
|||
|
#region 记录日志
|
|||
|
/// <summary>
|
|||
|
/// 根据路径创建文件
|
|||
|
/// </summary>
|
|||
|
/// <param name="realPath">文件路径</param>
|
|||
|
public static void CreateLog(string realPath)
|
|||
|
{
|
|||
|
StreamWriter SW;
|
|||
|
SW = File.CreateText(realPath);
|
|||
|
//SW.WriteLine(string.Format("支付宝回调: {0}", DateTime.Now.ToString("dd-MM-yyyy hh:mm:ss")));
|
|||
|
SW.Close();
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 向文件写入数据
|
|||
|
/// </summary>
|
|||
|
/// <param name="content">内容</param>
|
|||
|
/// <param name="realPath">文件路径</param>
|
|||
|
public static void WriteLog(string content, string realPath)
|
|||
|
{
|
|||
|
using (StreamWriter SW = File.AppendText(realPath))
|
|||
|
{
|
|||
|
SW.WriteLine(content);
|
|||
|
SW.Close();
|
|||
|
}
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
#region 获取虚拟目录根目录
|
|||
|
/// <summary>
|
|||
|
/// 获取虚拟目录根目录
|
|||
|
/// </summary>
|
|||
|
public static string SiteRoot
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
HttpRequest request = HttpContext.Current.Request;
|
|||
|
string root = request.ApplicationPath;
|
|||
|
if (root == "/") return root;
|
|||
|
else return root + "/";
|
|||
|
}
|
|||
|
}
|
|||
|
#endregion
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 文件上传操作结果返回类
|
|||
|
/// </summary>
|
|||
|
public class FileUpLoadMsgInfo
|
|||
|
{
|
|||
|
public FileUpLoadMsgInfo()
|
|||
|
{
|
|||
|
IsSuccess = true;
|
|||
|
FilePath = string.Empty;
|
|||
|
Msg = "操作成功";
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 是否上传成功
|
|||
|
/// </summary>
|
|||
|
public bool IsSuccess { get; set; }
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 上传好的文件路径
|
|||
|
/// </summary>
|
|||
|
public string FilePath { get; set; }
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 消息
|
|||
|
/// </summary>
|
|||
|
public string Msg { get; set; }
|
|||
|
}
|
|||
|
}
|