做了幾天的文件操作,現在來總結一下,錯誤之處,還望指點!以文件為例,如果對文件夾操作,基本上將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 }
