ASP.NET中的文件操作(文件信息,新建,移动,复制,重命名,上传,遍历)
做了几天的文件操作,现在来总结一下,错误之处,还望指点!以文件为例,如果对文件夹操作,基本上将File换为Directory即可(例:FileInfo file = new FileInfo(Path);与DirectoryInfo directory = new DirectoryInfo (Path);)
1获取文件信息
在知道文件相对路径的情形,下面代码可以获取文件的详细信息
1 public static void fileinfo(string Path)
2 {
3 Path = Server.MapPath(Path);//获取文件的物理路径
4 FileInfo file = new FileInfo(Path);//实例该路径文件信息
5 var length=file.Length;//文件大小,字节
6 var name = file.Name;//文件名
7 var fullname = file.FullName;//文件路径
8 var extension = file.Extension;//文件后缀名
9 ......
10 }
获取的信息还有创建时间,最后访问时间等等,可以自行研究
2新建文件
新建一个文件
1 public static void NewFile(string filePath)
2 {
3 filePath=Server.MapPath(filePath);//获取想创建文件的物理路径
4 if (System.IO.File.Exists(newfilepath))
5 {
6 //判断新建的文件是否已经存在
7 throw new Exception("文件已经存在")
8 }
9
10 System.IO.File.Create(newfilepath);//创建
11 ......
12 }
3复制文件,移动(剪切)文件,重命名文件
复制文件:
1 public static void Copy(string Path,string targetPath)
2 {
3 Path = Server.MapPath(Path);//原文件的物理路径
4 targetPath = Server.MapPath(targetPath);//复制到的新位置物理路径
5 //判断到的新地址是否存在重命名文件
6 if (System.IO.File.Exists(targetPath))
7 {
8 throw new Exception("存在同名文件");//抛出异常
9 }
10 System.IO.File.Copy(Path,targetPath);//复制到新位置,不允许覆盖现有文件
11 .......
12 }
移动文件,重命名:
1 public static void MoveOrRename(string Path,string targetPath)
2 {
3 Path = Server.MapPath(Path);//原文件的物理路径
4 targetPath = Server.MapPath(targetPath);//移动到的新位置的物理路径(如果还是当前文件夹,则会重命名文件)
5 //判断到的新地址是否存在重命名文件
6 if (System.IO.File.Exists(targetPath))
7 {
8 //判断是新位置是否存在同名(判断重命名是狗和其他文件冲突)
9 throw new Exception("已经存在同名文件");
10 }
11 System.IO.File.Move(Path,targetPath);//2个文件在不同目录则是移动,如果在相同目录下则是重命名
12 ......
13 }
复制文件不会删除,移动或者重命名(方法相同,就是目标位置不同)会删除原文件.
4上传文件
1 [HttpPost]//通过Post请求接收前台传来的文件数据
2 public ActionResult UploadFile(string dirPath)
3 {
4 var filepath = Server.MapPath(Path);//获取上传的文件存入目录的物理路径
5 var file = Request.Files["file"];//获取文件内容
6 if (file == null || file.ContentLength == 0)
7 {
8 throw new Exception("文件不存在");//简单判断下文件
9 }
10 var newfilepath = Server.MapPath(dirPath + "\\" + file.FileName);//获取文件名的物理路径
11 //判断要上传的文件是否与目录中的文件重命名
12 if (System.IO.File.Exists(newfilepath))
13 {
14 throw new Exception("文件不存在");//简单判断下文件是否存在
15 }
16 //文件存放到指定的文件中 ;
17 file.SaveAs(newfilepath);
18 ......
19 }
会自动创建存有该类容和命名的文件,不用多此一举去创建一个新文件再放入内容.
5遍历当前目录和其子目录所有文件
1 private static string[] GetFiles(string dir, string regexPattern = null, bool recurse = true, bool throwEx = false)
2 {
3 //recurse:是否递归
4 //throwEx:是否报出异常
5 List<string> lst = new List<string>();
6 try
7 {
8 foreach (string item in Directory.GetFileSystemEntries(dir))
9 {
10 try
11 {
12 bool isFile = (System.IO.File.GetAttributes(item) & FileAttributes.Directory) != FileAttributes.Directory;
13
14 if (isFile && (regexPattern == null || Regex.IsMatch(Path.GetFileName(item), regexPattern, RegexOptions.IgnoreCase | RegexOptions.Multiline)))
15 { lst.Add(item); }
16
17 //递归
18 if (recurse && !isFile) { lst.AddRange(GetFiles(item, regexPattern, true)); }
19 }
20 catch { if (throwEx) { throw; } }
21 }
22 }
23 catch { if (throwEx) { throw; } }
24
25 return lst.ToArray();
26 }

浙公网安备 33010602011771号