新闻资讯

asp.net文件与文件夹操作类(文件删除,创建,目录删除)

发布时间:2013-10-30

点击量:

下面这个文件操作类,可以删除目录并且不为空的目录哦,也可以创建文件写文件,删除文件以前递归操作目录。

using system.io;
using system.web;

namespace sec
{
    /××////
    /// 对文件和文件夹的操作类
    ///
    public class filecontrol
    {
         public filecontrol()
         {

         }
         /××////
         /// 在根目录下创建文件夹
         ///
         /// 要创建的文件路径
         public void createfolder(string folderpathname)
         {
             if(folderpathname.trim().length> 0)
             {
                 try
                 {
                      string createpath = system.web.httpcontext.current.server.mappath("http://www.cnblogs.com/images/%22+folderpathname).tostring();
                      if(!directory.exists(createpath))
                      {
                          directory.createdirectory(createpath);
                       }
                 }
                 catch
                 {
                       throw;
                  }
             }
         }

         /××////
         /// 删除一个文件夹下面的字文件夹和文件
         ///
         ///
         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;
                    }
             }
         }
         /××////
         /// 删除一个文件
         ///
         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
             {
                  //创建文件夹
                  string[] strpath= filepathname.split('/');
            &nbs


服务热线

服务热线

15383239821

微信咨询
微信二维码
返回顶部
×微信二维码

截屏,微信识别二维码

微信号:

(点击微信号复制,添加好友)

打开微信

微信号已复制,请打开微信添加咨询详情!