- 創建目錄和文件
1、通過Path類的Combine方法可以合並路徑。
string activeDir = @"C:\myDir"; string newPath = System.IO.Path.Combine(activeDir, "mySubDirOne");
2、目錄的創建。
創建目錄時如果目錄已存在,則不會重新創建目錄,且不會報錯。創建目錄時會自動創建路徑中各級不存在的目錄。
(1)通過Directory類的CreateDirectory方法創建。
string activeDir = @"C:\myDir"; string newPath = System.IO.Path.Combine(activeDir, "mySubDirOne"); System.IO.Directory.CreateDirectory(newPath);
(1)通過DirectoryInfo的對象創建。
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(@"C:\myDirTwo\mySubDirThree"); di.Create();
3、文件的創建。
通過Create方法創建文件,會覆蓋同名的現有文件。創建文件時,該文件所在路徑的目錄必須存在,否則報錯。
(1)通過File類的Create方法創建。
string activeDir = @"C:\myDir"; string newPath = System.IO.Path.Combine(activeDir, "mySubDirOne"); System.IO.Directory.CreateDirectory(newPath); //創建一個空白文件 string fileNameOne = DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".txt"; string filePathOne = System.IO.Path.Combine(newPath, fileNameOne); System.IO.File.Create(filePathOne);
(2)通過FileInfo對象創建。
//通過Combine合並目錄 //然后創建目錄 string activeDir = @"C:\myDir"; string newPath = System.IO.Path.Combine(activeDir, "mySubDirOne"); System.IO.Directory.CreateDirectory(newPath); //創建一個空白文件 string fileNameOne = DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".txt"; string filePathOne = System.IO.Path.Combine(newPath, fileNameOne); System.IO.FileInfo fi = new System.IO.FileInfo(filePathOne); fi.Create();
- 復制目錄文件
//復制單個文件到指定目錄 string fileName = "test.txt"; string sourcePath = @"C:\testDir\subTestDir"; string targetPath = @"C:\testDir\subTestDirTwo"; string sourceFile = System.IO.Path.Combine(sourcePath, fileName); string destFile = System.IO.Path.Combine(targetPath, fileName); if (!System.IO.Directory.Exists(targetPath)) System.IO.Directory.CreateDirectory(targetPath); //如果已存在,參數為false時將報錯,參數為true重寫該文件 //當copy方法為兩個參數時,默認重寫為false。 System.IO.File.Copy(sourceFile, destFile, true); //以下為復制一個目錄下所有文件到指定目錄 //如果復制有子目錄的目錄的所有文件,可以用遞歸或堆棧算法實現 if (System.IO.Directory.Exists(sourcePath)) { string[] files = System.IO.Directory.GetFiles(sourcePath); foreach (string s in files) { //僅返回路徑字符串的文件名及后綴 fileName = System.IO.Path.GetFileName(s); destFile = System.IO.Path.Combine(targetPath, fileName); System.IO.File.Copy(s, destFile, true); } } }
- 移動目錄和文件
/*移動文件*/ string sourceFile = @"C:\testDir\subTestDir\test.txt"; string destFile = @"C:\testDir\subTestDirTwo\test.txt"; //當目標文件存在時,拋出異常 System.IO.File.Move(sourceFile, destFile); /*移動目錄*/ //移動目錄將移動改目錄的子目錄和文件 System.IO.Directory.Move(@"C:\testDir\subTestDirTwo\", @"C:\testDir\subTestDir");
- 刪除目錄和文件
1、刪除目錄
刪除目錄,如果該目錄不存在,會拋出異常。可以通過File類的Delete方法刪除目錄,也可以通過FileInfo對象方法刪除目錄。
(1)通過 File類的Delete方法刪除目錄
//刪除可寫空目錄 //如果不為空拋出目錄不為空異常 try { System.IO.Directory.Delete(@"C:\testDir\subTestDir"); } catch (System.IO.IOException e) { Console.WriteLine(e.Message); } //第二參數為false時,只能刪除空目錄,否則拋出不為空異常 //第二參數為true時,刪除目錄,包括子目錄和文件 try { System.IO.Directory.Delete(@"C:\testDir\subTestDir", true); } catch(System.IO.IOException e) { Console.WriteLine(e.Message); }
(2)通過FileInfo對象方法刪除目錄
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(@"C:\testDir\subTestDirTwo"); try { //無參數刪除空目錄 //當參數為false,可刪除空目錄;為true,刪除目錄,包括子目錄和文件 di.Delete(true); } catch (System.IO.IOException e) { Console.WriteLine(e.Message); }
2、刪除文件
刪除文件時如果指定文件的目錄存在,而文件不存在,則不會拋出異常,如果指定文件的目錄不存在,則會拋出異常。
(1)通過File類Delete方法刪除文件
try { System.IO.File.Delete(@"C:\testDir\subTestDir\test.txt"); } catch(System.IO.IOException e) { Console.WriteLine(e.Message); }
(2)通過FileInfo對象Delete方法刪除文件
System.IO.FileInfo fi = new System.IO.FileInfo(@"C:\testDir\subTestDir\test1.txt"); try { fi.Delete(); } catch(System.IO.IOException e) { Console.WriteLine(e.Message); }