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