ASP.NET中的文件操做(文件信息,新建,移動,複製,重命名,上傳,遍歷)

作了幾天的文件操做,如今來總結一下,錯誤之處,還望指點!以文件爲例,若是對文件夾操做,基本上將File換爲Directory便可(例:FileInfo file = new FileInfo(Path);與DirectoryInfo directory = new DirectoryInfo (Path);)code

1獲取文件信息blog

在知道文件相對路徑的情形,下面代碼能夠獲取文件的詳細信息遞歸

 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 }

獲取的信息還有建立時間,最後訪問時間等等,能夠自行研究get

2新建文件string

新建一個文件it

 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複製文件,移動(剪切)文件,重命名文件io

複製文件:class

 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 }

移動文件,重命名:foreach

 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 }

複製文件不會刪除,移動或者重命名(方法相同,就是目標位置不一樣)會刪除原文件.List

 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         }
相關文章
相關標籤/搜索