国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

Asp.net對(duì)文件夾和文件的操作類

2024-07-10 13:07:06
字體:
供稿:網(wǎng)友
菜鳥學(xué)堂:

using system;
using system.io;
using system.web;

namespace sec
{
    /**//// <summary>
    /// 對(duì)文件和文件夾的操作類
    /// </summary>
    public class filecontrol
    {
        public filecontrol()
        {
           
        }
        /**//// <summary>
        /// 在根目錄下創(chuàng)建文件夾
        /// </summary>
        /// <param name="folderpath">要?jiǎng)?chuàng)建的文件路徑</param>
        public void createfolder(string folderpathname)
        {
            if(folderpathname.trim().length>0)
            {
                try
                {
                    string createpath = system.web.httpcontext.current.server.mappath

("../../../images/"+folderpathname).tostring();
                    if(!directory.exists(createpath))
                    {
                        directory.createdirectory(createpath);
                    }     
                }
                catch
                {
                    throw;
                }
            }
        }

        /**//// <summary>
        /// 刪除一個(gè)文件夾下面的字文件夾和文件
        /// </summary>
        /// <param name="folderpathname"></param>
        public void deletechildfolder(string folderpathname)
        {
            if(folderpathname.trim().length>0)
            {
                try
                {
                    string createpath = system.web.httpcontext.current.server.mappath

(folderpathname).tostring();
                    if(directory.exists(createpath))
                    {
                        directory.delete(createpath,true);
                    }     
                }
                catch
                {
                    throw;
                }
            }
        }

        /**//// <summary>
        /// 刪除一個(gè)文件
        /// </summary>
        /// <param name="filepathname"></param>
        public void deletefile(string filepathname)
        {
            try
            {
                fileinfo delefile = new fileinfo(system.web.httpcontext.current.server.mappath

(filepathname).tostring());
                delefile.delete();
            }
            catch
            {
            }
        }
        public void createfile(string filepathname)
        {
            try
            {
                //創(chuàng)建文件夾
                string[] strpath= filepathname.split('/');
                createfolder(filepathname.replace("/" + strpath[strpath.length-1].tostring(),"")); //創(chuàng)建文件


                fileinfo createfile =new fileinfo(system.web.httpcontext.current.server.mappath

(filepathname).tostring());         //創(chuàng)建文件
                if(!createfile.exists)
                {
                    filestream fs=createfile.create();     
                    fs.close();
                }
            }
            catch
            {
            }
        }
        /**//// <summary>
        /// 刪除整個(gè)文件夾及其字文件夾和文件
        /// </summary>
        /// <param name="folderpathname"></param>
        public void deleparentfolder(string folderpathname)
        {
            try
            {
                directoryinfo delfolder = new directoryinfo(system.web.httpcontext.current.server.mappath

(folderpathname).tostring());
                if(delfolder.exists)
                {
                    delfolder.delete(); 
                }
            }
            catch
            {
            }
        }
        /**//// <summary>
        /// 在文件里追加內(nèi)容
        /// </summary>
        /// <param name="filepathname"></param>
        public void rewritereadinnertext(string filepathname,string writeword)
        {
            try
            {
                //建立文件夾和文件
                //createfolder(filepathname);
                createfile(filepathname);
                //得到原來文件的內(nèi)容
                filestream fileread=new filestream(system.web.httpcontext.current.server.mappath

(filepathname).tostring(),filemode.open,fileaccess.readwrite);
                streamreader filereadword=new streamreader(fileread,system.text.encoding.default);
                string oldstring = filereadword.readtoend().tostring();
                oldstring = oldstring + writeword; 
                //把新的內(nèi)容重新寫入
                streamwriter filewrite=new streamwriter(fileread,system.text.encoding.default);
                filewrite.write(writeword);
                //關(guān)閉
                filewrite.close();
                filereadword.close();
                fileread.close();
            }
            catch
            {
                //    throw;
            }
        }

        /**//// <summary>
        /// 在文件里追加內(nèi)容
        /// </summary>
        /// <param name="filepathname"></param>
        public string readerfiledata(string filepathname)
        {
            try
            {
    
                filestream fileread=new filestream(system.web.httpcontext.current.server.mappath

(filepathname).tostring(),filemode.open,fileaccess.read);
                streamreader filereadword=new streamreader(fileread,system.text.encoding.default);
                string txtstring = filereadword.readtoend().tostring();                
                //關(guān)閉
                filereadword.close();
                fileread.close();
                return txtstring;
            }
            catch
            {
                throw;
            }
        }
        /**//// <summary>
        /// 讀取文件夾的文件
        /// </summary>
        /// <param name="filepathname"></param>
        /// <returns></returns>
        public directoryinfo checkvalidsessionpath(string filepathname)
        {               
            try
            {
                directoryinfo maindir = new directoryinfo(system.web.httpcontext.current.server.mappath

(filepathname));
                return maindir;    
               
            }
            catch
            {
                throw;
            }
        }
    }
}

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 阜新市| 洱源县| 平江县| 宝兴县| 楚雄市| 封丘县| 林甸县| 樟树市| 九江市| 治多县| 永寿县| 上林县| 阳信县| 大洼县| 咸宁市| 托克托县| 行唐县| 黄梅县| 恭城| 孟津县| 汶上县| 万山特区| 金山区| 渑池县| 慈利县| 京山县| 永定县| 辽源市| 门头沟区| 丰宁| 海丰县| 霍山县| 临沧市| 鹰潭市| 宁远县| 菏泽市| 龙井市| 逊克县| 台安县| 中西区| 仁寿县|