using System;<br />using System.Data;<br />using System.Configuration;<br />using System.Collections;<br />using System.Web;<br />using System.Web.Security;<br />using System.Web.UI;<br />using System.Web.UI.WebControls;<br />using System.Web.UI.WebControls.WebParts;<br />using System.Web.UI.HtmlControls;<br />using System.IO;<br />public partial class Default2 : System.Web.UI.Page<br />{<br /> protected void Page_Load(object sender, EventArgs e)<br /> {<br /> }<br /> #region 建立檔案夾<br /> /// <summary><br /> /// 建立檔案夾<br /> /// </summary><br /> /// <param name="Path"></param><br /> public static void FolderCreate(string Path)<br /> {<br /> // 判斷目標目錄是否存在如果不存在則建立之<br /> if (!Directory.Exists(Path))<br /> Directory.CreateDirectory(Path);<br /> }<br /> #endregion<br /> #region 建立目錄<br /> public static void FileCreate(string Path)<br /> {<br /> FileInfo CreateFile = new FileInfo(Path); //建立檔案<br /> if (!CreateFile.Exists)<br /> {<br /> FileStream FS = CreateFile.Create();<br /> FS.Close();<br /> }<br /> }<br /> #endregion<br /> #region 遞迴刪除檔案夾目錄及檔案<br /> /****************************************<br /> * 函數名稱:DeleteFolder<br /> * 功能說明:遞迴刪除檔案夾目錄及檔案<br /> * 參 數:dir:檔案夾路徑<br /> * 調用示列:<br /> * string dir = Server.MapPath("test/");<br /> * EC.FileObj.DeleteFolder(dir);<br /> *****************************************/<br /> /// <summary><br /> /// 遞迴刪除檔案夾目錄及檔案<br /> /// </summary><br /> /// <param name="dir"></param><br /> /// <returns></returns><br /> public static void DeleteFolder(string dir)<br /> {<br /> if (Directory.Exists(dir)) //如果存在這個檔案夾刪除之<br /> {<br /> foreach (string d in Directory.GetFileSystemEntries(dir))<br /> {<br /> if (File.Exists(d))<br /> File.Delete(d); //直接刪除其中的檔案<br /> else<br /> DeleteFolder(d); //遞迴刪除子檔案夾<br /> }<br /> Directory.Delete(dir, true); //刪除已空檔案夾<br /> }<br /> }<br /> #endregion<br /> #region 將指定檔案夾下面的所有內容copy到目標檔案夾下面 果目標檔案夾為唯讀屬性就會報錯。<br /> /****************************************<br /> * 函數名稱:CopyDir<br /> * 功能說明:將指定檔案夾下面的所有內容copy到目標檔案夾下面 果目標檔案夾為唯讀屬性就會報錯。<br /> * 參 數:srcPath:原始路徑,aimPath:目標檔案夾<br /> * 調用示列:<br /> * string srcPath = Server.MapPath("test/");<br /> * string aimPath = Server.MapPath("test1/");<br /> * EC.FileObj.CopyDir(srcPath,aimPath);<br /> *****************************************/<br /> /// <summary><br /> /// 指定檔案夾下面的所有內容copy到目標檔案夾下面<br /> /// </summary><br /> /// <param name="srcPath">原始路徑</param><br /> /// <param name="aimPath">目標檔案夾</param><br /> public static void CopyDir(string srcPath, string aimPath)<br /> {<br /> try<br /> {<br /> // 檢查目標目錄是否以目錄分割字元結束如果不是則添加之<br /> if (aimPath[aimPath.Length - 1] != Path.DirectorySeparatorChar)<br /> aimPath += Path.DirectorySeparatorChar;<br /> // 判斷目標目錄是否存在如果不存在則建立之<br /> if (!Directory.Exists(aimPath))<br /> Directory.CreateDirectory(aimPath);<br /> // 得到來源目錄的檔案清單,該裡面是包含檔案以及目錄路徑的一個數組<br /> //如果你指向copy目標檔案下面的檔案而不包含目錄請使用下面的方法<br /> //string[] fileList = Directory.GetFiles(srcPath);<br /> string[] fileList = Directory.GetFileSystemEntries(srcPath);<br /> //遍曆所有的檔案和目錄<br /> foreach (string file in fileList)<br /> {<br /> //先當作目錄處理如果存在這個目錄就遞迴Copy該目錄下面的檔案<br /> if (Directory.Exists(file))<br /> CopyDir(file, aimPath + Path.GetFileName(file));<br /> //否則直接Copy檔案<br /> else<br /> File.Copy(file, aimPath + Path.GetFileName(file), true);<br /> }<br /> }<br /> catch (Exception ee)<br /> {<br /> throw new Exception(ee.ToString());<br /> }<br /> }<br /> #endregion<br /> #region 擷取指定檔案夾下所有子目錄及檔案(樹形)<br /> /****************************************<br /> * 函數名稱:GetFoldAll(string Path)<br /> * 功能說明:擷取指定檔案夾下所有子目錄及檔案(樹形)<br /> * 參 數:Path:詳細路徑<br /> * 調用示列:<br /> * string strDirlist = Server.MapPath("templates");<br /> * this.Literal1.Text = EC.FileObj.GetFoldAll(strDirlist);<br /> *****************************************/<br /> /// <summary><br /> /// 擷取指定檔案夾下所有子目錄及檔案<br /> /// </summary><br /> /// <param name="Path">詳細路徑</param><br /> public static string GetFoldAll(string Path)<br /> {<br /> string str = "";<br /> DirectoryInfo thisOne = new DirectoryInfo(Path);<br /> str = ListTreeShow(thisOne, 0, str);<br /> return str;<br /> }<br /> /// <summary><br /> /// 擷取指定檔案夾下所有子目錄及檔案函數<br /> /// </summary><br /> /// <param name="theDir">指定目錄</param><br /> /// <param name="nLevel">預設起始值,調用時,一般為0</param><br /> /// <param name="Rn">用於迭加的傳入值,一般為空白</param><br /> /// <returns></returns><br /> public static string ListTreeShow(DirectoryInfo theDir, int nLevel, string Rn)//遞迴目錄 檔案<br /> {<br /> DirectoryInfo[] subDirectories = theDir.GetDirectories();//獲得目錄<br /> foreach (DirectoryInfo dirinfo in subDirectories)<br /> {<br /> if (nLevel == 0)<br /> {<br /> Rn += "├";<br /> }<br /> else<br /> {<br /> string _s = "";<br /> for (int i = 1; i <= nLevel; i++)<br /> {<br /> _s += "│ ";<br /> }<br /> Rn += _s + "├";<br /> }<br /> Rn += "<b>" + dirinfo.Name.ToString() + "</b><br />";<br /> FileInfo[] fileInfo = dirinfo.GetFiles(); //目錄下的檔案<br /> foreach (FileInfo fInfo in fileInfo)<br /> {<br /> if (nLevel == 0)<br /> {<br /> Rn += "│ ├";<br /> }<br /> else<br /> {<br /> string _f = "";<br /> for (int i = 1; i <= nLevel; i++)<br /> {<br /> _f += "│ ";<br /> }<br /> Rn += _f + "│ ├";<br /> }<br /> Rn += fInfo.Name.ToString() + " <br />";<br /> }<br /> Rn = ListTreeShow(dirinfo, nLevel + 1, Rn);</p><p> }<br /> return Rn;<br /> }</p><p> /****************************************<br /> * 函數名稱:GetFoldAll(string Path)<br /> * 功能說明:擷取指定檔案夾下所有子目錄及檔案(下拉框形)<br /> * 參 數:Path:詳細路徑<br /> * 調用示列:<br /> * string strDirlist = Server.MapPath("templates");<br /> * this.Literal2.Text = EC.FileObj.GetFoldAll(strDirlist,"tpl","");<br /> *****************************************/<br /> /// <summary><br /> /// 擷取指定檔案夾下所有子目錄及檔案(下拉框形)<br /> /// </summary><br /> /// <param name="Path">詳細路徑</param><br /> ///<param name="DropName">下拉式清單名稱</param><br /> ///<param name="tplPath">預設選擇模板名稱</param><br /> public static string GetFoldAll(string Path,string DropName,string tplPath)<br /> {<br /> string strDrop = "<select name=/"" + DropName + "/" id=/"" + DropName + "/"><option value=/"/">--請選擇詳細模板--</option>";<br /> string str = "";<br /> DirectoryInfo thisOne = new DirectoryInfo(Path);<br /> str = ListTreeShow(thisOne, 0, str,tplPath);<br /> return strDrop+str+"</select>";<br /> }<br /> /// <summary><br /> /// 擷取指定檔案夾下所有子目錄及檔案函數<br /> /// </summary><br /> /// <param name="theDir">指定目錄</param><br /> /// <param name="nLevel">預設起始值,調用時,一般為0</param><br /> /// <param name="Rn">用於迭加的傳入值,一般為空白</param><br /> /// <param name="tplPath">預設選擇模板名稱</param><br /> /// <returns></returns><br /> public static string ListTreeShow(DirectoryInfo theDir, int nLevel, string Rn,string tplPath)//遞迴目錄 檔案<br /> {<br /> DirectoryInfo[] subDirectories = theDir.GetDirectories();//獲得目錄<br /> foreach (DirectoryInfo dirinfo in subDirectories)<br /> {<br /> Rn += "<option value=/"" + dirinfo.Name.ToString() + "/"";<br /> if (tplPath.ToLower() == dirinfo.Name.ToString().ToLower())<br /> {<br /> Rn += " selected ";<br /> }<br /> Rn += ">";<br /> if (nLevel == 0)<br /> {<br /> Rn += "┣";<br /> }<br /> else<br /> {<br /> string _s = "";<br /> for (int i = 1; i <= nLevel; i++)<br /> {<br /> _s += "│ ";<br /> }<br /> Rn += _s + "┣";<br /> }<br /> Rn += "" + dirinfo.Name.ToString() + "</option>";</p><p> FileInfo[] fileInfo = dirinfo.GetFiles(); //目錄下的檔案<br /> foreach (FileInfo fInfo in fileInfo)<br /> {<br /> Rn += "<option value=/"" + dirinfo.Name.ToString()+"/"+fInfo.Name.ToString() + "/"";<br /> if (tplPath.ToLower() == fInfo.Name.ToString().ToLower())<br /> {<br /> Rn += " selected ";<br /> }<br /> Rn += ">";<br /> if (nLevel == 0)<br /> {<br /> Rn += "│ ├";<br /> }<br /> else<br /> {<br /> string _f = "";<br /> for (int i = 1; i <= nLevel; i++)<br /> {<br /> _f += "│ ";<br /> }<br /> Rn += _f + "│ ├";<br /> }<br /> Rn += fInfo.Name.ToString() + "</option>";<br /> }<br /> Rn = ListTreeShow(dirinfo, nLevel + 1, Rn, tplPath);</p><p> }<br /> return Rn;<br /> }<br /> #endregion<br /> #region 擷取檔案夾大小<br /> /****************************************<br /> * 函數名稱:GetDirectoryLength(string dirPath)<br /> * 功能說明:擷取檔案夾大小<br /> * 參 數:dirPath:檔案夾詳細路徑<br /> * 調用示列:<br /> * string Path = Server.MapPath("templates");<br /> * Response.Write(EC.FileObj.GetDirectoryLength(Path));<br /> *****************************************/<br /> /// <summary><br /> /// 擷取檔案夾大小<br /> /// </summary><br /> /// <param name="dirPath">檔案夾路徑</param><br /> /// <returns></returns><br /> public static long GetDirectoryLength(string dirPath)<br /> {<br /> if (!Directory.Exists(dirPath))<br /> return 0;<br /> long len = 0;<br /> DirectoryInfo di = new DirectoryInfo(dirPath);<br /> foreach (FileInfo fi in di.GetFiles())<br /> {<br /> len += fi.Length;<br /> }<br /> DirectoryInfo[] dis = di.GetDirectories();<br /> if (dis.Length > 0)<br /> {<br /> for (int i = 0; i < dis.Length; i++)<br /> {<br /> len += GetDirectoryLength(dis[i].FullName);<br /> }<br /> }<br /> return len;<br /> }<br /> #endregion<br /> #region 擷取指定檔案詳細屬性<br /> /****************************************<br /> * 函數名稱:GetFileAttibe(string filePath)<br /> * 功能說明:擷取指定檔案詳細屬性<br /> * 參 數:filePath:檔案詳細路徑<br /> * 調用示列:<br /> * string file = Server.MapPath("robots.txt");<br /> * Response.Write(EC.FileObj.GetFileAttibe(file));<br /> *****************************************/<br /> /// <summary><br /> /// 擷取指定檔案詳細屬性<br /> /// </summary><br /> /// <param name="filePath">檔案詳細路徑</param><br /> /// <returns></returns><br /> public static string GetFileAttibe(string filePath)<br /> {<br /> string str = "";<br /> System.IO.FileInfo objFI = new System.IO.FileInfo(filePath);<br /> str += "詳細路徑:" + objFI.FullName + "<br>檔案名稱:" + objFI.Name + "<br>檔案長度:" + objFI.Length.ToString() + "位元組<br>建立時間" + objFI.CreationTime.ToString() + "<br>最後訪問時間:" + objFI.LastAccessTime.ToString() + "<br>修改時間:" + objFI.LastWriteTime.ToString() + "<br>所在目錄:" + objFI.DirectoryName + "<br>副檔名:" + objFI.Extension;<br /> return str;<br /> }<br /> #endregion<br />}