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
{
///
/// 文件操作帮助类
///
public class Assistant
{
///
///删除指定目录下的所有文件
///
/// 目录
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
{
}
}
///
/// 在执行完导入操作后,删除导入过程中临时存放的导入文件
///
///
public static void DeleteFileOfImport(string importFilePath)
{
if (Convert.ToBoolean(ConfigurationManager.AppSettings["ImportPathDel"]))
{
//删除文件
try
{
System.IO.File.Delete(importFilePath);
}
catch { }
}
}
///
/// 检查是否存在某文件,存在则删除
///
/// 文件路径
public static void DeleteFile(string strFilePath)
{
if (File.Exists(strFilePath))
{
try
{
File.Delete(strFilePath);
}
catch { }
}
}
///
/// 上传文件
///
/// 上传控件
/// 文件路径
/// 文件上层文件夹路径
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);
}
///
/// 上传文件
///
/// 上传控件
/// 文件上层文件夹路径
/// 文件名称(默认为空)
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 从字符串里随机得到,规定个数的字符串
///
/// 从字符串里随机得到,规定个数的字符串.
///
///
///
///
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 记录日志
///
/// 根据路径创建文件
///
/// 文件路径
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();
}
///
/// 向文件写入数据
///
/// 内容
/// 文件路径
public static void WriteLog(string content, string realPath)
{
using (StreamWriter SW = File.AppendText(realPath))
{
SW.WriteLine(content);
SW.Close();
}
}
#endregion
#region 获取虚拟目录根目录
///
/// 获取虚拟目录根目录
///
public static string SiteRoot
{
get
{
HttpRequest request = HttpContext.Current.Request;
string root = request.ApplicationPath;
if (root == "/") return root;
else return root + "/";
}
}
#endregion
}
///
/// 文件上传操作结果返回类
///
public class FileUpLoadMsgInfo
{
public FileUpLoadMsgInfo()
{
IsSuccess = true;
FilePath = string.Empty;
Msg = "操作成功";
}
///
/// 是否上传成功
///
public bool IsSuccess { get; set; }
///
/// 上传好的文件路径
///
public string FilePath { get; set; }
///
/// 消息
///
public string Msg { get; set; }
}
}