日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網(wǎng)為廣大站長提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請做好本站友鏈:【 網(wǎng)站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

對于文件流的操作,首先你得引用命名空間:using System.IO;對文件的操作主要指兩方面:第一,是對文件本身進(jìn)行操作;第二,是對文件內(nèi)容進(jìn)行操作。

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
    ...{
        構(gòu)造函數(shù)#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

        IDisposable 成員#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("http://www.cnblogs.com/說明.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

        在當(dāng)前目錄下創(chuàng)建目錄#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

        創(chuàng)建目錄#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

        將指定文件夾下面的所有內(nèi)容copy到目標(biāo)文件夾下面 果目標(biāo)文件夾為只讀屬性就會報(bào)錯。#region 將指定文件夾下面的所有內(nèi)容copy到目標(biāo)文件夾下面 果目標(biāo)文件夾為只讀屬性就會報(bào)錯。
        /**//****************************************
        * 函數(shù)名稱:CopyDir
        * 功能說明:將指定文件夾下面的所有內(nèi)容copy到目標(biāo)文件夾下面 果目標(biāo)文件夾為只讀屬性就會報(bà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:詳細(xì)路徑
        * 調(diào)用示列:
        *          string strDirlist = Server.MapPath("templates");      
        *          this.Literal1.Text = EC.FileObj.GetFoldAll(strDirlist);  
        *****************************************/
        /**//// <summary>
        /// 獲取指定文件夾下所有子目錄及文件
        /// </summary>
        /// <param name="Path">詳細(xì)路徑</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">默認(rèn)起始值,調(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 += "│&nbsp;";
                    }
                    Rn += _s + "├";
                }
                Rn += "<b>" + dirinfo.Name.ToString() + "</b><br />";
                FileInfo[] fileInfo = dirinfo.GetFiles();  //目錄下的文件
                foreach (FileInfo fInfo in fileInfo)
                ...{
                    if (nLevel == 0)
                    ...{
                        Rn += "│&nbsp;├";
                    }
                    else
                    ...{
                        string _f = "";
                        for (int i = 1; i <= nLevel; i++)
                        ...{
                            _f += "│&nbsp;";
                        }
                        Rn += _f + "│&nbsp;├";
                    }
                    Rn += fInfo.Name.ToString() + " <br />";
                }
                Rn = ListTreeShow(dirinfo, nLevel + 1, Rn);


            }
            return Rn;
        }

        /**//****************************************
        * 函數(shù)名稱:GetFoldAll(string Path)
        * 功能說明:獲取指定文件夾下所有子目錄及文件(下拉框形)
        * 參    數(shù):Path:詳細(xì)路徑
        * 調(diào)用示列:
        *            string strDirlist = Server.MapPath("templates");      
        *            this.Literal2.Text = EC.FileObj.GetFoldAll(strDirlist,"tpl","");
        *****************************************/
        /**//// <summary>
        /// 獲取指定文件夾下所有子目錄及文件(下拉框形)
        /// </summary>
        /// <param name="Path">詳細(xì)路徑</param>
        ///<param name="DropName">下拉列表名稱</param>
        ///<param name="tplPath">默認(rèn)選擇模板名稱</param>
        public static string GetFoldAll(string Path,string DropName,string tplPath)
        ...{
            string strDrop = "<select name="" + DropName + "" id="" + DropName + ""><option value="">--請選擇詳細(xì)模板--</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">默認(rèn)起始值,調(diào)用時,一般為0</param>
        /// <param name="Rn">用于迭加的傳入值,一般為空</param>
        /// <param name="tplPath">默認(rèn)選擇模板名稱</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 += "│&nbsp;";
                    }
                    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 += "│&nbsp;├";
                    }
                    else
                    ...{
                        string _f = "";
                        for (int i = 1; i <= nLevel; i++)
                        ...{
                            _f += "│&nbsp;";
                        }
                        Rn += _f + "│&nbsp;├";
                    }
                    Rn += fInfo.Name.ToString() + "</option>";
                }
                Rn = ListTreeShow(dirinfo, nLevel + 1, Rn, tplPath);


            }
            return Rn;
        }
        #endregion

        獲取文件夾大小#region 獲取文件夾大小
        /**//****************************************
        * 函數(shù)名稱:GetDirectoryLength(string dirPath)
        * 功能說明:獲取文件夾大小
        * 參    數(shù):dirPath:文件夾詳細(xì)路徑
        * 調(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.FullName);
                }
            }
            return len;
        }
        #endregion

        獲取指定文件詳細(xì)屬性#region 獲取指定文件詳細(xì)屬性
        /**//****************************************
        * 函數(shù)名稱:GetFileAttibe(string filePath)
        * 功能說明:獲取指定文件詳細(xì)屬性
        * 參    數(shù):filePath:文件詳細(xì)路徑
        * 調(diào)用示列:
        * string file = Server.MapPath("robots.txt");  
        * Response.Write(EC.FileObj.GetFileAttibe(file));        
        *****************************************/
        /**//// <summary>
        /// 獲取指定文件詳細(xì)屬性
        /// </summary>
        /// <param name="filePath">文件詳細(xì)路徑</param>
        /// <returns></returns>
        public static string GetFileAttibe(string filePath)
        ...{
            string str = "";
            System.IO.FileInfo objFI = new System.IO.FileInfo(filePath);
            str += "詳細(xì)路徑:" + 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>擴(kuò)展名:" + objFI.Extension;
            return str;
        }
        #endregion
    }
}

 

分享到:
標(biāo)簽:刪除 拷貝 批量 函數(shù) 讀取
用戶無頭像

網(wǎng)友整理

注冊時間:

網(wǎng)站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學(xué)四六

運(yùn)動步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績評定2018-06-03

通用課目體育訓(xùn)練成績評定