一区二区三区日韩精品-日韩经典一区二区三区-五月激情综合丁香婷婷-欧美精品中文字幕专区

分享

asp.net 對文件和文件夾操作

 實力決定地位 2010-12-31
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;
namespace EC
{
    /// <summary>
    /// FileObj 的摘要說明
    /// </summary>
    public class FileObj
    {
        #region 構(gòu)造函數(shù)
        private bool _alreadyDispose = false;
        public FileObj()
        {
            //
            // TODO: 在此處添加構(gòu)造函數(shù)邏輯
            //
        }
        ~FileObj()
        {
            Dispose(); ;
        }
        protected virtual void Dispose(bool isDisposing)
        {
            if (_alreadyDispose) return;
            //if (isDisposing)
            //{
            //    if (xml != null)
            //    {
            //        xml = null;
            //    }
            //}
            _alreadyDispose = true;
        }
        #endregion
        #region IDisposable 成員
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        #endregion
        #region 取得文件后綴名
        /****************************************
         * 函數(shù)名稱:GetPostfixStr
         * 功能說明:取得文件后綴名
         * 參    數(shù):filename:文件名稱
         * 調(diào)用示列:
         *           string filename = "aaa.aspx";       
         *           string s = EC.FileObj.GetPostfixStr(filename);        
        *****************************************/
        /// <summary>
        /// 取后綴名
        /// </summary>
        /// <param name="filename">文件名</param>
        /// <returns>.gif|.html格式</returns>
        public static string GetPostfixStr(string filename)
        {
            int start = filename.LastIndexOf(".");
            int length = filename.Length;
            string postfix = filename.Substring(start, length - start);
            return postfix;
        }
        #endregion
        #region 寫文件
        /****************************************
         * 函數(shù)名稱:WriteFile
         * 功能說明:當(dāng)文件不存時,則創(chuàng)建文件,并追加文件
         * 參    數(shù):Path:文件路徑,Strings:文本內(nèi)容
         * 調(diào)用示列:
         *           string Path = Server.MapPath("Default2.aspx");      
         *           string Strings = "這是我寫的內(nèi)容啊";
         *           EC.FileObj.WriteFile(Path,Strings);
        *****************************************/
        /// <summary>
        /// 寫文件
        /// </summary>
        /// <param name="Path">文件路徑</param>
        /// <param name="Strings">文件內(nèi)容</param>
        public static void WriteFile(string Path, string Strings)
        {
            if (!System.IO.File.Exists(Path))
            {
                //Directory.CreateDirectory(Path);
                System.IO.FileStream f = System.IO.File.Create(Path);
                f.Close();
                f.Dispose();
            }
            System.IO.StreamWriter f2 = new System.IO.StreamWriter(Path, true, System.Text.Encoding.UTF8);
            f2.WriteLine(Strings);
            f2.Close();
            f2.Dispose();

        }
        #endregion
        #region 讀文件
        /****************************************
         * 函數(shù)名稱:ReadFile
         * 功能說明:讀取文本內(nèi)容
         * 參    數(shù):Path:文件路徑
         * 調(diào)用示列:
         *           string Path = Server.MapPath("Default2.aspx");      
         *           string s = EC.FileObj.ReadFile(Path);
        *****************************************/
        /// <summary>
        /// 讀文件
        /// </summary>
        /// <param name="Path">文件路徑</param>
        /// <returns></returns>
        public static string ReadFile(string Path)
        {
            string s = "";
            if (!System.IO.File.Exists(Path))
                s = "不存在相應(yīng)的目錄";
            else
            {
                StreamReader f2 = new StreamReader(Path, System.Text.Encoding.GetEncoding("gb2312"));
                s = f2.ReadToEnd();
                f2.Close();
                f2.Dispose();
            }
            return s;
        }
        #endregion
        #region 追加文件
        /****************************************
         * 函數(shù)名稱:FileAdd
         * 功能說明:追加文件內(nèi)容
         * 參    數(shù):Path:文件路徑,strings:內(nèi)容
         * 調(diào)用示列:
         *           string Path = Server.MapPath("Default2.aspx");    
         *           string Strings = "新追加內(nèi)容";
         *           EC.FileObj.FileAdd(Path, Strings);
        *****************************************/
        /// <summary>
        /// 追加文件
        /// </summary>
        /// <param name="Path">文件路徑</param>
        /// <param name="strings">內(nèi)容</param>
        public static void FileAdd(string Path, string strings)
        {
            StreamWriter sw = File.AppendText(Path);
            sw.Write(strings);
            sw.Flush();
            sw.Close();
            sw.Dispose();
        }
        #endregion
        #region 拷貝文件
        /****************************************
         * 函數(shù)名稱:FileCoppy
         * 功能說明:拷貝文件
         * 參    數(shù):OrignFile:原始文件,NewFile:新文件路徑
         * 調(diào)用示列:
         *           string OrignFile = Server.MapPath("Default2.aspx");    
         *           string NewFile = Server.MapPath("Default3.aspx");
         *           EC.FileObj.FileCoppy(OrignFile, NewFile);
        *****************************************/
        /// <summary>
        /// 拷貝文件
        /// </summary>
        /// <param name="OrignFile">原始文件</param>
        /// <param name="NewFile">新文件路徑</param>
        public static void FileCoppy(string OrignFile, string NewFile)
        {
            File.Copy(OrignFile, NewFile, true);
        }
        #endregion
        #region 刪除文件
        /****************************************
         * 函數(shù)名稱:FileDel
         * 功能說明:刪除文件
         * 參    數(shù):Path:文件路徑
         * 調(diào)用示列:
         *           string Path = Server.MapPath("Default3.aspx");   
         *           EC.FileObj.FileDel(Path);
        *****************************************/
        /// <summary>
        /// 刪除文件
        /// </summary>
        /// <param name="Path">路徑</param>
        public static void FileDel(string Path)
        {
            File.Delete(Path);
        }
        #endregion
        #region 移動文件
        /****************************************
         * 函數(shù)名稱:FileMove
         * 功能說明:移動文件
         * 參    數(shù):OrignFile:原始路徑,NewFile:新文件路徑
         * 調(diào)用示列:
         *            string OrignFile = Server.MapPath("../說明.txt");   
         *            string NewFile = Server.MapPath("../../說明.txt");
         *            EC.FileObj.FileMove(OrignFile, NewFile);
        *****************************************/
        /// <summary>
        /// 移動文件
        /// </summary>
        /// <param name="OrignFile">原始路徑</param>
        /// <param name="NewFile">新路徑</param>
        public static void FileMove(string OrignFile, string NewFile)
        {
            File.Move(OrignFile, NewFile);
        }
        #endregion
        #region 在當(dāng)前目錄下創(chuàng)建目錄
        /****************************************
         * 函數(shù)名稱:FolderCreate
         * 功能說明:在當(dāng)前目錄下創(chuàng)建目錄
         * 參    數(shù):OrignFolder:當(dāng)前目錄,NewFloder:新目錄
         * 調(diào)用示列:
         *           string OrignFolder = Server.MapPath("test/");   
         *           string NewFloder = "new";
         *           EC.FileObj.FolderCreate(OrignFolder, NewFloder);
        *****************************************/
        /// <summary>
        /// 在當(dāng)前目錄下創(chuàng)建目錄
        /// </summary>
        /// <param name="OrignFolder">當(dāng)前目錄</param>
        /// <param name="NewFloder">新目錄</param>
        public static void FolderCreate(string OrignFolder, string NewFloder)
        {
            Directory.SetCurrentDirectory(OrignFolder);
            Directory.CreateDirectory(NewFloder);
        }
        /// <summary>
        /// 創(chuàng)建文件夾
        /// </summary>
        /// <param name="Path"></param>
        public static void FolderCreate(string Path)
        {
            // 判斷目標(biāo)目錄是否存在如果不存在則新建之
            if (!Directory.Exists(Path))
                Directory.CreateDirectory(Path);
        }
        #endregion
        #region 創(chuàng)建目錄
        public static void FileCreate(string Path)
        {
            FileInfo CreateFile = new FileInfo(Path); //創(chuàng)建文件
            if (!CreateFile.Exists)
            {
                FileStream FS = CreateFile.Create();
                FS.Close();
            }
        }
        #endregion
        #region 遞歸刪除文件夾目錄及文件
        /****************************************
         * 函數(shù)名稱:DeleteFolder
         * 功能說明:遞歸刪除文件夾目錄及文件
         * 參    數(shù):dir:文件夾路徑
         * 調(diào)用示列:
         *           string dir = Server.MapPath("test/"); 
         *           EC.FileObj.DeleteFolder(dir);      
        *****************************************/
        /// <summary>
        /// 遞歸刪除文件夾目錄及文件
        /// </summary>
        /// <param name="dir"></param> 
        /// <returns></returns>
        public static void DeleteFolder(string dir)
        {
            if (Directory.Exists(dir)) //如果存在這個文件夾刪除之
            {
                foreach (string d in Directory.GetFileSystemEntries(dir))
                {
                    if (File.Exists(d))
                        File.Delete(d); //直接刪除其中的文件                       
                    else
                        DeleteFolder(d); //遞歸刪除子文件夾
                }
                Directory.Delete(dir, true); //刪除已空文件夾                
            }
        }
        #endregion
        #region 將指定文件夾下面的所有內(nèi)容copy到目標(biāo)文件夾下面 果目標(biāo)文件夾為只讀屬性就會報錯。
        /****************************************
         * 函數(shù)名稱:CopyDir
         * 功能說明:將指定文件夾下面的所有內(nèi)容copy到目標(biāo)文件夾下面 果目標(biāo)文件夾為只讀屬性就會報錯。
         * 參    數(shù):srcPath:原始路徑,aimPath:目標(biāo)文件夾
         * 調(diào)用示列:
         *           string srcPath = Server.MapPath("test/"); 
         *           string aimPath = Server.MapPath("test1/");
         *           EC.FileObj.CopyDir(srcPath,aimPath);  
        *****************************************/
        /// <summary>
        /// 指定文件夾下面的所有內(nèi)容copy到目標(biāo)文件夾下面
        /// </summary>
        /// <param name="srcPath">原始路徑</param>
        /// <param name="aimPath">目標(biāo)文件夾</param>
        public static void CopyDir(string srcPath, string aimPath)
        {
            try
            {
                // 檢查目標(biāo)目錄是否以目錄分割字符結(jié)束如果不是則添加之
                if (aimPath[aimPath.Length - 1] != Path.DirectorySeparatorChar)
                    aimPath += Path.DirectorySeparatorChar;
                // 判斷目標(biāo)目錄是否存在如果不存在則新建之
                if (!Directory.Exists(aimPath))
                    Directory.CreateDirectory(aimPath);
                // 得到源目錄的文件列表,該里面是包含文件以及目錄路徑的一個數(shù)組
                //如果你指向copy目標(biāo)文件下面的文件而不包含目錄請使用下面的方法
                //string[] fileList = Directory.GetFiles(srcPath);
                string[] fileList = Directory.GetFileSystemEntries(srcPath);
                //遍歷所有的文件和目錄
                foreach (string file in fileList)
                {
                    //先當(dāng)作目錄處理如果存在這個目錄就遞歸Copy該目錄下面的文件
                    if (Directory.Exists(file))
                        CopyDir(file, aimPath + Path.GetFileName(file));
                    //否則直接Copy文件
                    else
                        File.Copy(file, aimPath + Path.GetFileName(file), true);
                }
            }
            catch (Exception ee)
            {
                throw new Exception(ee.ToString());
            }
        }
        #endregion
        #region 獲取指定文件夾下所有子目錄及文件(樹形)
        /****************************************
         * 函數(shù)名稱:GetFoldAll(string Path)
         * 功能說明:獲取指定文件夾下所有子目錄及文件(樹形)
         * 參    數(shù):Path:詳細路徑
         * 調(diào)用示列:
         *           string strDirlist = Server.MapPath("templates");      
         *           this.Literal1.Text = EC.FileObj.GetFoldAll(strDirlist); 
        *****************************************/
        /// <summary>
        /// 獲取指定文件夾下所有子目錄及文件
        /// </summary>
        /// <param name="Path">詳細路徑</param>
        public static string GetFoldAll(string Path)
        {
            string str = "";
            DirectoryInfo thisOne = new DirectoryInfo(Path);
            str = ListTreeShow(thisOne, 0, str);
            return str;
        }
        /// <summary>
        /// 獲取指定文件夾下所有子目錄及文件函數(shù)
        /// </summary>
        /// <param name="theDir">指定目錄</param>
        /// <param name="nLevel">默認起始值,調(diào)用時,一般為0</param>
        /// <param name="Rn">用于迭加的傳入值,一般為空</param>
        /// <returns></returns>
        public static string ListTreeShow(DirectoryInfo theDir, int nLevel, string Rn)//遞歸目錄 文件
        {
            DirectoryInfo[] subDirectories = theDir.GetDirectories();//獲得目錄
            foreach (DirectoryInfo dirinfo in subDirectories)
            {
                if (nLevel == 0)
                {
                    Rn += "├";
                }
                else
                {
                    string _s = "";
                    for (int i = 1; i <= nLevel; i++)
                    {
                        _s += "│ ";
                    }
                    Rn += _s + "├";
                }
                Rn += "<b>" + dirinfo.Name.ToString() + "</b><br />";
                FileInfo[] fileInfo = dirinfo.GetFiles();   //目錄下的文件
                foreach (FileInfo fInfo in fileInfo)
                {
                    if (nLevel == 0)
                    {
                        Rn += "│ ├";
                    }
                    else
                    {
                        string _f = "";
                        for (int i = 1; i <= nLevel; i++)
                        {
                            _f += "│ ";
                        }
                        Rn += _f + "│ ├";
                    }
                    Rn += fInfo.Name.ToString() + " <br />";
                }
                Rn = ListTreeShow(dirinfo, nLevel + 1, Rn);

            }
            return Rn;
        }
 
        /****************************************
         * 函數(shù)名稱:GetFoldAll(string Path)
         * 功能說明:獲取指定文件夾下所有子目錄及文件(下拉框形)
         * 參    數(shù):Path:詳細路徑
         * 調(diào)用示列:
         *            string strDirlist = Server.MapPath("templates");     
         *            this.Literal2.Text = EC.FileObj.GetFoldAll(strDirlist,"tpl","");
        *****************************************/
        /// <summary>
        /// 獲取指定文件夾下所有子目錄及文件(下拉框形)
        /// </summary>
        /// <param name="Path">詳細路徑</param>
        ///<param name="DropName">下拉列表名稱</param>
        ///<param name="tplPath">默認選擇模板名稱</param>
        public static string GetFoldAll(string Path,string DropName,string tplPath)
        {
            string strDrop = "<select name=\"" + DropName + "\" id=\"" + DropName + "\"><option value=\"\">--請選擇詳細模板--</option>";
            string str = "";
            DirectoryInfo thisOne = new DirectoryInfo(Path);
            str = ListTreeShow(thisOne, 0, str,tplPath);
            return strDrop+str+"</select>";
        }
        /// <summary>
        /// 獲取指定文件夾下所有子目錄及文件函數(shù)
        /// </summary>
        /// <param name="theDir">指定目錄</param>
        /// <param name="nLevel">默認起始值,調(diào)用時,一般為0</param>
        /// <param name="Rn">用于迭加的傳入值,一般為空</param>
        /// <param name="tplPath">默認選擇模板名稱</param>
        /// <returns></returns>
        public static string ListTreeShow(DirectoryInfo theDir, int nLevel, string Rn,string tplPath)//遞歸目錄 文件
        {
            DirectoryInfo[] subDirectories = theDir.GetDirectories();//獲得目錄
            foreach (DirectoryInfo dirinfo in subDirectories)
            {
                Rn += "<option value=\"" + dirinfo.Name.ToString() + "\"";
                if (tplPath.ToLower() == dirinfo.Name.ToString().ToLower())
                {
                    Rn += " selected ";
                }
                Rn += ">";
                if (nLevel == 0)
                {
                    Rn += "┣";
                }
                else
                {
                    string _s = "";
                    for (int i = 1; i <= nLevel; i++)
                    {
                        _s += "│ ";
                    }
                    Rn += _s + "┣";
                }
                Rn += "" + dirinfo.Name.ToString() + "</option>";

                FileInfo[] fileInfo = dirinfo.GetFiles();   //目錄下的文件
                foreach (FileInfo fInfo in fileInfo)
                {
                    Rn += "<option value=\"" + dirinfo.Name.ToString()+"/"+fInfo.Name.ToString() + "\"";
                    if (tplPath.ToLower() == fInfo.Name.ToString().ToLower())
                    {
                        Rn += " selected ";
                    }
                    Rn += ">";
                    if (nLevel == 0)
                    {
                        Rn += "│ ├";
                    }
                    else
                    {
                        string _f = "";
                        for (int i = 1; i <= nLevel; i++)
                        {
                            _f += "│ ";
                        }
                        Rn += _f + "│ ├";
                    }
                    Rn += fInfo.Name.ToString() + "</option>";
                }
                Rn = ListTreeShow(dirinfo, nLevel + 1, Rn, tplPath);

            }
            return Rn;
        }
        #endregion
        #region 獲取文件夾大小
        /****************************************
         * 函數(shù)名稱:GetDirectoryLength(string dirPath)
         * 功能說明:獲取文件夾大小
         * 參    數(shù):dirPath:文件夾詳細路徑
         * 調(diào)用示列:
         *           string Path = Server.MapPath("templates");
         *           Response.Write(EC.FileObj.GetDirectoryLength(Path));      
        *****************************************/
        /// <summary>
        /// 獲取文件夾大小
        /// </summary>
        /// <param name="dirPath">文件夾路徑</param>
        /// <returns></returns>
        public static long GetDirectoryLength(string dirPath)
        {
            if (!Directory.Exists(dirPath))
                return 0;
            long len = 0;
            DirectoryInfo di = new DirectoryInfo(dirPath);
            foreach (FileInfo fi in di.GetFiles())
            {
                len += fi.Length;
            }
            DirectoryInfo[] dis = di.GetDirectories();
            if (dis.Length > 0)
            {
                for (int i = 0; i < dis.Length; i++)
                {
                    len += GetDirectoryLength(dis[i].FullName);
                }
            }
            return len;
        }
        #endregion
        #region 獲取指定文件詳細屬性
        /****************************************
         * 函數(shù)名稱:GetFileAttibe(string filePath)
         * 功能說明:獲取指定文件詳細屬性
         * 參    數(shù):filePath:文件詳細路徑
         * 調(diào)用示列:
         *           string file = Server.MapPath("robots.txt"); 
         *            Response.Write(EC.FileObj.GetFileAttibe(file));        
        *****************************************/
        /// <summary>
        /// 獲取指定文件詳細屬性
        /// </summary>
        /// <param name="filePath">文件詳細路徑</param>
        /// <returns></returns>
        public static string GetFileAttibe(string filePath)
        {
            string str = "";
            System.IO.FileInfo objFI = new System.IO.FileInfo(filePath);
            str += "詳細路徑:" + objFI.FullName + "<br>文件名稱:" + objFI.Name + "<br>文件長度:" + objFI.Length.ToString() + "字節(jié)<br>創(chuàng)建時間" + objFI.CreationTime.ToString() + "<br>最后訪問時間:" + objFI.LastAccessTime.ToString() + "<br>修改時間:" + objFI.LastWriteTime.ToString() + "<br>所在目錄:" + objFI.DirectoryName + "<br>擴展名:" + objFI.Extension;
            return str;
        }
        #endregion
    }
}

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多

    亚洲熟女国产熟女二区三区| 欧美一区二区三区十区| 精品伊人久久大香线蕉综合| 91插插插外国一区二区| 午夜精品一区免费视频| 亚洲综合香蕉在线视频| 丁香六月啪啪激情综合区| 欧美区一区二区在线观看| 好吊妞视频这里有精品| 国产女性精品一区二区三区| 亚洲妇女作爱一区二区三区| 自拍偷女厕所拍偷区亚洲综合| 国产日韩综合一区在线观看| 美女露小粉嫩91精品久久久| 麻豆亚州无矿码专区视频| 欧美日韩国产精品第五页| 日韩毛片视频免费观看| 亚洲精品熟女国产多毛| 国产麻豆视频一二三区| 国产精品一区二区不卡中文| 国产福利在线播放麻豆| 日本欧美一区二区三区高清| 欧美日韩有码一二三区| 欧美日韩精品久久第一页| 91超频在线视频中文字幕| 色婷婷日本视频在线观看| 中文字幕高清不卡一区| 国产老熟女超碰一区二区三区| 欧美日韩精品久久亚洲区熟妇人| 护士又紧又深又湿又爽的视频| 国产麻豆一区二区三区在| 1024你懂的在线视频| 欧美黑人在线精品极品| 国产精品制服丝袜美腿丝袜| 欧美成人黄色一级视频| 欧美日韩一级黄片免费观看 | 国产不卡的视频在线观看| 国产传媒精品视频一区| 日本和亚洲的香蕉视频| 国产精品免费无遮挡不卡视频| 日本美国三级黄色aa|