好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

HDHCMS为您提供ASP.NET(?C#)中文件操作类及方法

做了几天的文件操作,现在来总结一下,错误之处,还望指点!以文件为例,如果对文件夹操作,基本上将File换为Directory即可(例:FileInfo file = new FileInfo(Path);与DirectoryInfo directory = new DirectoryInfo (Path);)


1获取文件信息


在知道文件相对路径的情形,下面代码可以获取文件的详细信息


           public static void fileinfo(string Path)

         {

             Path = Server.MapPath(Path);//获取文件的物理路径

             FileInfo file = new FileInfo(Path);//实例该路径文件信息

             var length=file.Length;//文件大小,字节

             var name = file.Name;//文件名

             var fullname = file.FullName;//文件路径

             var extension = file.Extension;//文件后缀名

             ......

          }

获取的信息还有创建时间,最后访问时间等等,可以自行研究


2新建文件


新建一个文件


       public static void NewFile(string filePath)

         {

             filePath=Server.MapPath(filePath);//获取想创建文件的物理路径

             if (System.IO.File.Exists(newfilepath))

             {

                 //判断新建的文件是否已经存在

                 throw new Exception("文件已经存在")

             }

 

             System.IO.File.Create(newfilepath);//创建

             ......

         }

3复制文件,移动(剪切)文件,重命名文件


复制文件:


         public static void Copy(string Path,string targetPath)

         {

             Path = Server.MapPath(Path);//原文件的物理路径

             targetPath = Server.MapPath(targetPath);//复制到的新位置物理路径

             //判断到的新地址是否存在重命名文件

             if (System.IO.File.Exists(targetPath))

             {

                 throw new Exception("存在同名文件");//抛出异常

             }        

              System.IO.File.Copy(Path,targetPath);//复制到新位置,不允许覆盖现有文件

             .......

         }

移动文件,重命名:


      public static void MoveOrRename(string Path,string targetPath)

         {

             Path = Server.MapPath(Path);//原文件的物理路径

             targetPath = Server.MapPath(targetPath);//移动到的新位置的物理路径(如果还是当前文件夹,则会重命名文件)

             //判断到的新地址是否存在重命名文件

             if (System.IO.File.Exists(targetPath))

             {

                 //判断是新位置是否存在同名(判断重命名是狗和其他文件冲突)

                 throw new Exception("已经存在同名文件");

             }

             System.IO.File.Move(Path,targetPath);//2个文件在不同目录则是移动,如果在相同目录下则是重命名

             ......

         }

复制文件不会删除,移动或者重命名(方法相同,就是目标位置不同)会删除原文件.


 4上传文件


         [HttpPost]//通过Post请求接收前台传来的文件数据

         public ActionResult UploadFile(string dirPath)

         {

             var filepath = Server.MapPath(Path);//获取上传的文件存入目录的物理路径

             var file = Request.Files["file"];//获取文件内容

             if (file == null || file.ContentLength == 0)

             {

                 throw new Exception("文件不存在");//简单判断下文件

             }

             var newfilepath = Server.MapPath(dirPath + "\" + file.FileName);//获取文件名的物理路径

             //判断要上传的文件是否与目录中的文件重命名

             if (System.IO.File.Exists(newfilepath))

             {

                 throw new Exception("文件不存在");//简单判断下文件是否存在

             }

             //文件存放到指定的文件中 ;

             file.SaveAs(newfilepath);

             ......

         }

会自动创建存有该类容和命名的文件,不用多此一举去创建一个新文件再放入内容.


 5遍历当前目录和其子目录所有文件


       private static string[] GetFiles(string dir, string regexPattern = null, bool recurse = true, bool throwEx = false)

         {

             //recurse:是否递归

             //throwEx:是否报出异常

             List<string> lst = new List<string>();

             try

             {

                 foreach (string item in Directory.GetFileSystemEntries(dir))

                 {

                     try

                     {

                         bool isFile = (System.IO.File.GetAttributes(item) & FileAttributes.Directory) != FileAttributes.Directory;

 

                         if (isFile && (regexPattern == null || Regex.IsMatch(Path.GetFileName(item), regexPattern, RegexOptions.IgnoreCase | RegexOptions.Multiline)))

                         { lst.Add(item); }

 

                         //递归

                         if (recurse && !isFile) { lst.AddRange(GetFiles(item, regexPattern, true)); }

                     }

                     catch { if (throwEx) { throw; } }

                 }

             }

             catch { if (throwEx) { throw; } }

 

             return lst.ToArray();

         }

 这个不多说,网上找到的代码,亲测有效.


查看更多关于HDHCMS为您提供ASP.NET(?C#)中文件操作类及方法的详细内容...

  阅读:2508次