C#操做目錄和文件html
建立目錄和文件算法
一、經過Path類的Combine方法能夠合併路徑。ide
string activeDir = @"C:\myDir"; string newPath = System.IO.Path.Combine(activeDir, "mySubDirOne");
二、目錄的建立。post
建立目錄時若是目錄已存在,則不會從新建立目錄,且不會報錯。建立目錄時會自動建立路徑中各級不存在的目錄。
(1)經過Directory類的CreateDirectory方法建立。url
string activeDir = @"C:\myDir"; string newPath = System.IO.Path.Combine(activeDir, "mySubDirOne"); System.IO.Directory.CreateDirectory(newPath);
(1)經過DirectoryInfo的對象建立。spa
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(@"C:\myDirTwo\mySubDirThree"); di.Create();
三、文件的建立。htm
經過Create方法建立文件,會覆蓋同名的現有文件。建立文件時,該文件所在路徑的目錄必須存在,不然報錯。對象
(1)經過File類的Create方法建立。blog
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");
刪除目錄和文件
一、刪除目錄
刪除目錄,若是該目錄不存在,會拋出異常。能夠經過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); }
二、刪除文件
刪除文件時若是指定文件的目錄存在,而文件不存在,則不會拋出異常,若是指定文件的目錄不存在,則會拋出異常。
(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); }