C#操作目錄與文件的方法步驟
更新時(shí)間:2013年05月30日 16:37:02 作者:
本篇文章是對(duì)C#操作目錄與文件的方法步驟進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
• 創(chuàng)建目錄和文件
1、通過(guò)Path類的Combine方法可以合并路徑。
string activeDir = @"C:\myDir";
string newPath = System.IO.Path.Combine(activeDir, "mySubDirOne");
2、目錄的創(chuàng)建。
創(chuàng)建目錄時(shí)如果目錄已存在,則不會(huì)重新創(chuàng)建目錄,且不會(huì)報(bào)錯(cuò)。創(chuàng)建目錄時(shí)會(huì)自動(dòng)創(chuàng)建路徑中各級(jí)不存在的目錄。
(1)通過(guò)Directory類的CreateDirectory方法創(chuàng)建。
string activeDir = @"C:\myDir";
string newPath = System.IO.Path.Combine(activeDir, "mySubDirOne");
System.IO.Directory.CreateDirectory(newPath);
(2)通過(guò)DirectoryInfo的對(duì)象創(chuàng)建。
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(@"C:\myDirTwo\mySubDirThree");
di.Create();
3、文件的創(chuàng)建。
通過(guò)Create方法創(chuàng)建文件,會(huì)覆蓋同名的現(xiàn)有文件。創(chuàng)建文件時(shí),該文件所在路徑的目錄必須存在,否則報(bào)錯(cuò)。
(1)通過(guò)File類的Create方法創(chuàng)建。
string activeDir = @"C:\myDir";
string newPath = System.IO.Path.Combine(activeDir, "mySubDirOne");
System.IO.Directory.CreateDirectory(newPath);
//創(chuàng)建一個(gè)空白文件
string fileNameOne = DateTime.Now.ToString("yyyyMMddHHmmssffff")
+ ".txt";
string filePathOne = System.IO.Path.Combine(newPath, fileNameOne);
System.IO.File.Create(filePathOne);
(2)通過(guò)FileInfo對(duì)象創(chuàng)建。
//通過(guò)Combine合并目錄
//然后創(chuàng)建目錄
string activeDir = @"C:\myDir";
string newPath = System.IO.Path.Combine(activeDir, "mySubDirOne");
System.IO.Directory.CreateDirectory(newPath);
//創(chuàng)建一個(gè)空白文件
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();
• 復(fù)制目錄文件
//復(fù)制單個(gè)文件到指定目錄
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);
//如果已存在,參數(shù)為false時(shí)將報(bào)錯(cuò),參數(shù)為true重寫該文件
//當(dāng)copy方法為兩個(gè)參數(shù)時(shí),默認(rèn)重寫為false。
System.IO.File.Copy(sourceFile, destFile, true);
//以下為復(fù)制一個(gè)目錄下所有文件到指定目錄
//如果復(fù)制有子目錄的目錄的所有文件,可以用遞歸或堆棧算法實(shí)現(xiàn)
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);
}
}
}
• 移動(dòng)目錄和文件
/*移動(dòng)文件*/
string sourceFile = @"C:\testDir\subTestDir\test.txt";
string destFile = @"C:\testDir\subTestDirTwo\test.txt";
//當(dāng)目標(biāo)文件存在時(shí),拋出異常
System.IO.File.Move(sourceFile, destFile);
/*移動(dòng)目錄*/
//移動(dòng)目錄將移動(dòng)改目錄的子目錄和文件
System.IO.Directory.Move(@"C:\testDir\subTestDirTwo\", @"C:\testDir\subTestDir");
• 刪除目錄和文件
1、刪除目錄
刪除目錄,如果該目錄不存在,會(huì)拋出異常。可以通過(guò)File類的Delete方法刪除目錄,也可以通過(guò)FileInfo對(duì)象方法刪除目錄。
(1)通過(guò) File類的Delete方法刪除目錄
//刪除可寫空目錄
//如果不為空拋出目錄不為空異常
try
{
System.IO.Directory.Delete(@"C:\testDir\subTestDir");
}
catch (System.IO.IOException e)
{
Console.WriteLine(e.Message);
}
//第二參數(shù)為false時(shí),只能刪除空目錄,否則拋出不為空異常
//第二參數(shù)為true時(shí),刪除目錄,包括子目錄和文件
try
{
System.IO.Directory.Delete(@"C:\testDir\subTestDir", true);
}
catch(System.IO.IOException e)
{
Console.WriteLine(e.Message);
}
(2)通過(guò)FileInfo對(duì)象方法刪除目錄
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(@"C:\testDir\subTestDirTwo");
try
{
//無(wú)參數(shù)刪除空目錄
//當(dāng)參數(shù)為false,可刪除空目錄;為true,刪除目錄,包括子目錄和文件
di.Delete(true);
}
catch (System.IO.IOException e)
{
Console.WriteLine(e.Message);
}
2、刪除文件
刪除文件時(shí)如果指定文件的目錄存在,而文件不存在,則不會(huì)拋出異常,如果指定文件的目錄不存在,則會(huì)拋出異常。
(1)通過(guò)File類Delete方法刪除文件
try
{
System.IO.File.Delete(@"C:\testDir\subTestDir\test.txt");
}
catch(System.IO.IOException e)
{
Console.WriteLine(e.Message);
}
(2)通過(guò)FileInfo對(duì)象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);
}
1、通過(guò)Path類的Combine方法可以合并路徑。
復(fù)制代碼 代碼如下:
string activeDir = @"C:\myDir";
string newPath = System.IO.Path.Combine(activeDir, "mySubDirOne");
2、目錄的創(chuàng)建。
創(chuàng)建目錄時(shí)如果目錄已存在,則不會(huì)重新創(chuàng)建目錄,且不會(huì)報(bào)錯(cuò)。創(chuàng)建目錄時(shí)會(huì)自動(dòng)創(chuàng)建路徑中各級(jí)不存在的目錄。
(1)通過(guò)Directory類的CreateDirectory方法創(chuàng)建。
復(fù)制代碼 代碼如下:
string activeDir = @"C:\myDir";
string newPath = System.IO.Path.Combine(activeDir, "mySubDirOne");
System.IO.Directory.CreateDirectory(newPath);
(2)通過(guò)DirectoryInfo的對(duì)象創(chuàng)建。
復(fù)制代碼 代碼如下:
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(@"C:\myDirTwo\mySubDirThree");
di.Create();
3、文件的創(chuàng)建。
通過(guò)Create方法創(chuàng)建文件,會(huì)覆蓋同名的現(xiàn)有文件。創(chuàng)建文件時(shí),該文件所在路徑的目錄必須存在,否則報(bào)錯(cuò)。
(1)通過(guò)File類的Create方法創(chuàng)建。
復(fù)制代碼 代碼如下:
string activeDir = @"C:\myDir";
string newPath = System.IO.Path.Combine(activeDir, "mySubDirOne");
System.IO.Directory.CreateDirectory(newPath);
//創(chuàng)建一個(gè)空白文件
string fileNameOne = DateTime.Now.ToString("yyyyMMddHHmmssffff")
+ ".txt";
string filePathOne = System.IO.Path.Combine(newPath, fileNameOne);
System.IO.File.Create(filePathOne);
(2)通過(guò)FileInfo對(duì)象創(chuàng)建。
復(fù)制代碼 代碼如下:
//通過(guò)Combine合并目錄
//然后創(chuàng)建目錄
string activeDir = @"C:\myDir";
string newPath = System.IO.Path.Combine(activeDir, "mySubDirOne");
System.IO.Directory.CreateDirectory(newPath);
//創(chuàng)建一個(gè)空白文件
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();
• 復(fù)制目錄文件
復(fù)制代碼 代碼如下:
//復(fù)制單個(gè)文件到指定目錄
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);
//如果已存在,參數(shù)為false時(shí)將報(bào)錯(cuò),參數(shù)為true重寫該文件
//當(dāng)copy方法為兩個(gè)參數(shù)時(shí),默認(rèn)重寫為false。
System.IO.File.Copy(sourceFile, destFile, true);
//以下為復(fù)制一個(gè)目錄下所有文件到指定目錄
//如果復(fù)制有子目錄的目錄的所有文件,可以用遞歸或堆棧算法實(shí)現(xiàn)
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);
}
}
}
• 移動(dòng)目錄和文件
復(fù)制代碼 代碼如下:
/*移動(dòng)文件*/
string sourceFile = @"C:\testDir\subTestDir\test.txt";
string destFile = @"C:\testDir\subTestDirTwo\test.txt";
//當(dāng)目標(biāo)文件存在時(shí),拋出異常
System.IO.File.Move(sourceFile, destFile);
/*移動(dòng)目錄*/
//移動(dòng)目錄將移動(dòng)改目錄的子目錄和文件
System.IO.Directory.Move(@"C:\testDir\subTestDirTwo\", @"C:\testDir\subTestDir");
• 刪除目錄和文件
1、刪除目錄
刪除目錄,如果該目錄不存在,會(huì)拋出異常。可以通過(guò)File類的Delete方法刪除目錄,也可以通過(guò)FileInfo對(duì)象方法刪除目錄。
(1)通過(guò) File類的Delete方法刪除目錄
復(fù)制代碼 代碼如下:
//刪除可寫空目錄
//如果不為空拋出目錄不為空異常
try
{
System.IO.Directory.Delete(@"C:\testDir\subTestDir");
}
catch (System.IO.IOException e)
{
Console.WriteLine(e.Message);
}
//第二參數(shù)為false時(shí),只能刪除空目錄,否則拋出不為空異常
//第二參數(shù)為true時(shí),刪除目錄,包括子目錄和文件
try
{
System.IO.Directory.Delete(@"C:\testDir\subTestDir", true);
}
catch(System.IO.IOException e)
{
Console.WriteLine(e.Message);
}
(2)通過(guò)FileInfo對(duì)象方法刪除目錄
復(fù)制代碼 代碼如下:
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(@"C:\testDir\subTestDirTwo");
try
{
//無(wú)參數(shù)刪除空目錄
//當(dāng)參數(shù)為false,可刪除空目錄;為true,刪除目錄,包括子目錄和文件
di.Delete(true);
}
catch (System.IO.IOException e)
{
Console.WriteLine(e.Message);
}
2、刪除文件
刪除文件時(shí)如果指定文件的目錄存在,而文件不存在,則不會(huì)拋出異常,如果指定文件的目錄不存在,則會(huì)拋出異常。
(1)通過(guò)File類Delete方法刪除文件
復(fù)制代碼 代碼如下:
try
{
System.IO.File.Delete(@"C:\testDir\subTestDir\test.txt");
}
catch(System.IO.IOException e)
{
Console.WriteLine(e.Message);
}
(2)通過(guò)FileInfo對(duì)象Delete方法刪除文件
復(fù)制代碼 代碼如下:
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);
}
相關(guān)文章
C#中按引用傳遞與按值傳遞的區(qū)別,以及ref與out關(guān)鍵字的用法詳解
以下是對(duì)C#中按引用傳遞與按值傳遞的區(qū)別,以及ref與out關(guān)鍵字的用法進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過(guò)來(lái)參考下2013-07-07