封裝了對文件類的各種操作
轉載自http://www.open-open.com/lib/view/open1391348397082.html
代碼如下:
using System; using System.Data; using System.Configuration; using System.Linq; using System.Web; using System.IO; using System.Collections; using System.Collections.Generic; using System.Security.AccessControl; using System.Security.Permissions; namespace Bll { public class FolderHelper { //判斷文件夾是否存在 public static bool checkFolderExits(string path) { DirectoryInfo dir = new DirectoryInfo(path); if (dir.Exists)//文件夾存在 { return true; } else { //dir.Create();//不存在就創建一個 return false; } } //創建一個文件夾,存在就創建失敗 public static bool CreateNewFolder(string path) { DirectoryInfo dir = new DirectoryInfo(path); if (!dir.Exists) { dir.Create(); return true; } else return false; } /// <summary> /// 在指定目錄下創建指定名稱文件夾 /// </summary> /// <param name="ParentsPath"></param> /// <param name="NewFolderName"></param> /// <returns></returns> public static bool CreateNewFolder(string ParentsPath, string NewFolderName) { string CreatePath = ParentsPath + @"\" + NewFolderName; DirectoryInfo dir = new DirectoryInfo(CreatePath); if (!dir.Exists) { dir.Create(); return true; } else return false; } /// <summary> /// 返回目錄下的所有文件名 /// </summary> /// <param name="path"></param> /// <returns></returns> public static ArrayList getAllFiles(string path) { DirectoryInfo dir = new DirectoryInfo(path); if (dir.Exists) { FileInfo[] fileinfo = dir.GetFiles(); ArrayList list = new ArrayList(); foreach (FileInfo f in fileinfo) { list.Add(f.Name); } return list; } else return null; } /// <summary> /// 計算文件夾的大小 /// </summary> /// <param name="d"></param> /// <returns></returns> public static long DirSize(DirectoryInfo d) { long Size = 0; // Add file sizes. FileInfo[] fis = d.GetFiles();//獲得目錄文件列表 foreach (FileInfo fi in fis) { Size += fi.Length; } // Add subdirectory sizes. DirectoryInfo[] dis = d.GetDirectories();//獲取目錄子目錄列表 foreach (DirectoryInfo di in dis) { Size += DirSize(di); } return Size; } /// <summary> /// 把文件夾得大小轉換成比較合適的表示單位 /// </summary> /// <param name="size"></param> /// <returns></returns> public static string ViewSize(long size) { long m=size; string viewstr; if ((m / 1024) > 0)//表示可以轉換成KB { m = m / 1024;//轉換成KB if ((m / 1024) > 0)//表示可以轉換成MB { m = m / 1024;//轉換成MB了 if ((m / 1024) > 0)//表示可以轉換成GB { m = m / 1024;//轉換成GB了 viewstr = m.ToString() + "GB"; } else { viewstr = m.ToString() + "MB"; } } else { viewstr = m.ToString() + "KB"; } } else { viewstr = m.ToString() + "byte"; } return viewstr; } /// <summary> /// 刪除指定目錄和內容 /// </summary> /// <param name="dir"></param> /// <returns></returns> public static bool delDir(string dir) { bool flag = false; DirectoryInfo d = new DirectoryInfo(dir); if (d.Exists)//判斷目錄是否存在 { try { d.Delete(); flag = true; } catch (Exception e) { flag = false; } } return flag; } /// <summary> /// 刪除指定文件 /// </summary> /// <param name="fil"></param> /// <returns></returns> public static bool delFile(string fil) { bool flag = false; FileInfo d = new FileInfo(fil); if (d.Exists)//判斷目錄是否存在 { try { d.Delete(); flag = true; } catch (Exception e) { flag = false; } } return flag; } public static void Copy(string sourceDirectory, string targetDirectory) { DirectoryInfo diSource = new DirectoryInfo(sourceDirectory); DirectoryInfo diTarget = new DirectoryInfo(targetDirectory); CopyAll(diSource, diTarget); } /// <summary> /// 復制目錄及子文件到指定目錄 /// </summary> /// <param name="source"></param> /// <param name="target"></param> public static void CopyAll(DirectoryInfo source, DirectoryInfo target) { // Check if the target directory exists, if not, create it. if (Directory.Exists(target.FullName) == false) { Directory.CreateDirectory(target.FullName); } // Copy each file into it's new directory. foreach (FileInfo fi in source.GetFiles()) { Console.WriteLine(@"Copying {0}\{1}", target.FullName, fi.Name); fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true); } // Copy each subdirectory using recursion. foreach (DirectoryInfo diSourceSubDir in source.GetDirectories()) { DirectoryInfo nextTargetSubDir = target.CreateSubdirectory(diSourceSubDir.Name); CopyAll(diSourceSubDir, nextTargetSubDir); } } /// <summary> /// 循環讀取某個目錄下的所有文件和目錄,查詢有關每一項的一些信息。返回一個文件列表 /// </summary> /// <param name="strCurrentDir"></param> public static List<fileEntity> FileView(string strCurrentDir) { List<fileEntity> fileList = new List<fileEntity>(); DirectoryInfo dir = new DirectoryInfo(strCurrentDir); foreach (FileSystemInfo fsi in dir.GetFileSystemInfos())//這個循環再讀取文件的信息 { try { //FileSystemInfo 對象可以表示文件或目錄,從而可以作為 FileInfo 或 DirectoryInfo 對象的基礎。 當分析許多文件和目錄時,請使用該基類。 FileInfo fi; DirectoryInfo di; //創建一個自己寫的實體類的實體 fileEntity newfile = new fileEntity(); if (fsi is FileInfo)//外層循環讀取文件信息 { //表示當前fsi是文件 fi = (FileInfo)fsi; newfile.fileName = fi.Name; newfile.fileExt = fi.Extension; newfile.fileSize = fi.Length; newfile.FileModify = fi.LastWriteTime; //通過擴展名來選擇文件顯示圖標 switch (newfile.fileExt) { case ".gif": newfile.picName = "gif.gif"; break; default: newfile.picName = "other.gif"; break; } newfile.picName = "<img src='" + newfile.picName + "' width=25 height=20>"; } else { di = (DirectoryInfo)fsi; newfile.fileName = di.Name; newfile.fileSize = DirSize(di);//調用計算文件夾大小的方法 newfile.FileModify = di.LastWriteTime; newfile.picName = "<img src='directory.gif' width=25 height=20>"; } fileList.Add(newfile); } catch (Exception e) { } } return fileList; } /// <summary> /// 顯示目錄和文件 /// </summary> /// <param name="path"></param> public static void All(string path) { FileInfo fi;//文件 DirectoryInfo di;//目錄 DirectoryInfo dir=null; int i = 0; //控制行的顏色 try { dir = new DirectoryInfo(path); } catch (Exception e) { } foreach (FileSystemInfo fsi in dir.GetFileSystemInfos()) { try { fileEntity newfile = new fileEntity(); FolderEntity folder = new FolderEntity(); newfile.fileName = ""; newfile.picName = ""; newfile.fileExt = ""; newfile.fileSize = 0; folder.folderName = ""; folder.picName = ""; i += 1; if (fsi is FileInfo)//判斷當前讀取的是不是一個文件 { //表示當前fsi是文件 fi = (FileInfo)fsi; newfile.fileName = fi.Name; newfile.fileExt = fi.Extension; newfile.fileSize = fi.Length; newfile.FileModify = fi.LastWriteTime; //將文件加上可以下載文件的鏈接 newfile.fileName = "<a href='........'></a>"; //通過擴展名來選擇文件顯示圖標 //Response.Write(Session["DataBasePath"].ToString()+"\\filetype\\"+pfun.getFileExt(FileExt)+".gif"); if (fsi.Exists) { switch (newfile.fileExt) { case ".gif": newfile.picName = "gif.gif"; break; default: newfile.picName = "other.gif"; break; } } else { newfile.picName = "unknown.gif"; } /* switch(FileExt) { case ".gif": FilePic = "gif.gif"; break; default: FilePic = "other.gif"; break; } */ newfile.picName = "<img src='filetype/" + newfile.picName + "' width=16 height=16>"; } else { //當前為目錄 di = (DirectoryInfo)fsi; folder.folderName = di.Name; //給目錄加上鏈接 folder.folderName = "<a href='.......'><a>"; folder.lastTime = di.LastWriteTime; folder.picName = "<img src='filetype/folder.gif' width=16 height=16>"; } }catch(Exception e){} } } } } View Code