分類:
Asp.Net
2009-08-03 11:57
89人閱讀
收藏
舉報(bào)
- public class FileOperBase
- {
-
-
-
-
-
- public static string WebPathTran(string path)
- {
- try
- {
- return HttpContext.Current.Server.MapPath(path);
- }
- catch
- {
- return path;
- }
-
- }
- }
-
- ------------------
-
-
-
- public struct FileMessage
- {
-
-
-
- public int FileSize;
-
-
-
- public string FileName;
- }
- ---------------------------
-
-
-
- public class FileOper : FileOperBase
- {
-
-
-
-
-
-
-
-
- public static FileMessage UploadFile(FileUpload fileUploadControl, string SavePath, bool IsGuid)
- {
- FileMessage Message = new FileMessage();
- string filename = fileUploadControl.FileName;
- if (IsGuid)
- {
-
- 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;
- }
-
- }
-
-
-
-
- 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 + "http://" + 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); }
- }
-
-
-
-
-
-
-
-
- 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;
- }
- }
-
-
-
-
-
-
-
- public static bool BackupFile(string sourceFileName, string destFileName)
- {
- return BackupFile(sourceFileName, destFileName, true);
- }
-
-
-
-
-
-
-
-
- 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);
- }
-
-
-
-
-
-
-
- public static bool DeleteFile(string parentPath, string filename)
- {
- try
- {
- string filefullname = WebPathTran(parentPath) + filename;
- File.Delete(filefullname);
- return true;
- }
- catch
- {
- return false;
- }
-
- }
-
-
-
-
-
- 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;
- }
-
-
-
-
-
- public static bool ExistsFile(string FilePath)
- {
- return File.Exists(WebPathTran(FilePath));
- }
-
-
-
-
-
- public static bool ExistsFold(string Path)
- {
- return Directory.Exists(WebPathTran(Path));
- }
-
-
-
-
- public static void CreateFold(string Path)
- {
- Directory.CreateDirectory(WebPathTran(Path));
- }
-
- }
|