Biegral 2019/1/4 9:52:00 1188
public class FileOperBase { /// <summary> /// 路径转换(转换成物理路径) /// </summary> /// <param name="path"></param> /// <returns></returns> public static string WebPathTran(string path) { try { return HttpContext.Current.Server.MapPath(path); } catch { return path; } } } ------------------ /// <summary> /// 文件信息 /// </summary> public struct FileMessage { /// <summary> /// 文件大小 /// </summary> public int FileSize; /// <summary> /// 文件名 /// </summary> public string FileName; } --------------------------- /// <summary> /// 文件操作 /// </summary> public class FileOper : FileOperBase { /// <summary> /// 上传文件(默认文件名) /// </summary> /// <param name="fileUploadControl">上传控件ID</param> /// <param name="SavePath">指定目录 相对路径</param> /// <param name="IsGuid">启用Guid重命名</param> /// <returns></returns> public static FileMessage UploadFile(FileUpload fileUploadControl, string SavePath, bool IsGuid) { FileMessage Message = new FileMessage(); string filename = fileUploadControl.FileName; if (IsGuid) { //Guid+扩展名 filename = System.Guid.NewGuid().ToString() + filename.Substring(filename.LastIndexOf(".")); } try { string FileInServerName = WebPathTran(SavePath) + filename; Message.FileSize = fileUploadControl.PostedFile.ContentLength; Message.FileName = filename; fileUploadControl.SaveAs(FileInServerName); return Message; } catch (Exception ex) { throw ex; } } /// <summary> /// 文件下载 /// </summary> /// <param name="filefullname">完整文件名 相对路径</param> public static void DownLoadSmallFile(string filefullname) { HttpResponse response = HttpContext.Current.Response; HttpRequest request = HttpContext.Current.Request; string filepath = filefullname.Substring(0, filefullname.LastIndexOf("/") + 1); string filename = filefullname.Remove(0, filepath.Length); string Fileparentpath = WebPathTran(filepath); try { if (File.Exists(Fileparentpath + "//" + filename)) { FileStream f = new FileStream(WebPathTran(filefullname), FileMode.Open, FileAccess.Read); byte[] data = new byte[f.Length]; f.Read(data, 0, (int)f.Length); response.Clear(); response.ClearHeaders(); response.Buffer = false; response.ContentType = "application/octet-stream"; response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8)); response.AppendHeader("Content-Length", data.Length.ToString()); f.Close(); response.BinaryWrite(data); } else { response.Redirect(request.ServerVariables["HTTP_REFERER"]); } } catch (Exception ew) { response.Write(ew.Message); } } /// <summary> /// 备份文件 /// </summary> /// <param name="sourceFileName">源文件名 物理路径</param> /// <param name="destFileName">目标文件名 物理路径</param> /// <param name="overwrite">当目标文件存在时是否覆盖</param> /// <returns>操作是否成功</returns> public static bool BackupFile(string sourceFileName, string destFileName, bool overwrite) { if (!System.IO.File.Exists(sourceFileName)) { throw new FileNotFoundException(sourceFileName + "文件不存在!"); } if (!overwrite && System.IO.File.Exists(destFileName)) { return false; } try { System.IO.File.Copy(sourceFileName, destFileName, true); return true; } catch (Exception e) { throw e; } } /// <summary> /// 备份文件,当目标文件存在时覆盖 /// </summary> /// <param name="sourceFileName">源文件名 物理路径</param> /// <param name="destFileName">目标文件名 物理路径</param> /// <returns>操作是否成功</returns> public static bool BackupFile(string sourceFileName, string destFileName) { return BackupFile(sourceFileName, destFileName, true); } /// <summary> /// 恢复文件 /// </summary> /// <param name="backupFileName">备份文件名 物理路径 </param> /// <param name="targetFileName">要恢复的文件名 物理路径</param> /// <param name="backupTargetFileName">要恢复文件再次备份的名称,如果为null,则不再备份恢复文件</param> /// <returns>操作是否成功</returns> public static bool RestoreFile(string backupFileName, string targetFileName, string backupTargetFileName) { try { if (!System.IO.File.Exists(backupFileName)) { throw new FileNotFoundException(backupFileName + "文件不存在!"); } if (backupTargetFileName != null) { if (!System.IO.File.Exists(targetFileName)) { throw new FileNotFoundException(targetFileName + "文件不存在!无法备份此文件!"); } else { System.IO.File.Copy(targetFileName, backupTargetFileName, true); } } System.IO.File.Delete(targetFileName); System.IO.File.Copy(backupFileName, targetFileName); } catch (Exception e) { throw e; } return true; } public static bool RestoreFile(string backupFileName, string targetFileName) { return RestoreFile(backupFileName, targetFileName, null); } /// <summary> /// 文件删除 /// </summary> /// <param name="parentPath">所在父级目录</param> /// <param name="filename">文件名</param> /// <returns></returns> public static bool DeleteFile(string parentPath, string filename) { try { string filefullname = WebPathTran(parentPath) + filename; File.Delete(filefullname); return true; } catch { return false; } } /// <summary> /// 文件删除 /// </summary> /// <param name="FilePath"></param> /// <returns></returns> public static bool DeleteFile(string FilePath) { FileInfo fi = new FileInfo(WebPathTran(FilePath)); if (fi.Exists) { try { fi.Delete(); return true; } catch { return false; } } return true; } /// <summary> /// 判断文件是否存在 /// </summary> /// <param name="FilePath"></param> /// <returns></returns> public static bool ExistsFile(string FilePath) { return File.Exists(WebPathTran(FilePath)); } /// <summary> /// 判断文件夹是否存在 /// </summary> /// <param name="Path"></param> /// <returns></returns> public static bool ExistsFold(string Path) { return Directory.Exists(WebPathTran(Path)); } /// <summary> /// 创建文件夹 /// </summary> /// <param name="Path"></param> public static void CreateFold(string Path) { Directory.CreateDirectory(WebPathTran(Path)); } }