C# 對檔案與檔案夾的操作包括刪除、移動與複製

來源:互聯網
上載者:User

在.Net中,對檔案(File)和檔案夾(Folder)的操作可以使用File類和Directory類,也可以使用FileInfo類和DirectoryInfo類。檔案夾(Folder)是只在Windows作業系統中使用的名詞。在作業系統的理論中,人們更習慣於使用目錄(Directory)這個名詞。或許微軟為了有朝一日將.Net移植到其他的作業系統中(實際上也有很多人也在做著這個項目),所以還是以Directory來命名操作檔案夾的類。

File類和Directory類都是靜態類。使用它們的好處是不需要初始化對象。如果你對某一個檔案或檔案夾只進行一次操作,那你最好使用該靜態類的靜態方法,比如File.Move,File.Delete等等。如果你需要對一個檔案或檔案夾進行多次操作,那最好還是使用FileInfo和DirectoryInfo類。因為File類和Directory是靜態類,所以你每次對一個檔案或檔案夾進行操作之前,它們都需要對該檔案或檔案夾進行一些檢查,比如authentication。如果使用FileInfo類和DirectoryInfo類,只在初始化類的對象時進行相關的檢查工作,也就是說只需要做一次,所以如果你需要對某個檔案或檔案夾進行多次操作,那最好使用FileInfo類和DirectoryInfo類。

下面的這段代碼示範了如何獲得檔案夾的資訊,包括獲得檔案夾下的子檔案夾,以及檔案夾下的檔案。這裡使用了DirectoryInfo 類來完成,當然你也可以使用Directory靜態類。 複製代碼 代碼如下:void DisplayFolder()
{
string folderFullName = @"c:\temp";
DirectoryInfo theFolder = new DirectoryInfo(folderFullName);
if (!theFolder.Exists)
throw new DirectoryNotFoundException("Folder not found: " + folderFullName);
// list all subfolders in folder
Console.WriteLine("Subfolders:");
foreach (DirectoryInfo subFolder in theFolder.GetDirectories())
{
Console.WriteLine(subFolder.Name);
}
// list all files in folder
Console.WriteLine();
Console.WriteLine("Files:");
foreach (FileInfo file in theFolder.GetFiles())
{
Console.WriteLine(file.Name);
}
}

下面示範了如何使用FileInfo類來獲得檔案的相關資訊,包括檔案的建立日期,檔案的大小等等。當然你同樣也可以使用File靜態類來完成。 複製代碼 代碼如下:void DisplayFileInfo()
{
string folderFullName = @"c:\temp";
string fileName = "New Text Document.txt";
string fileFullName = Path.Combine(folderFullName, fileName);
FileInfo theFile = new FileInfo(fileFullName);
if (!theFile.Exists)
throw new FileNotFoundException("File not found: " + fileFullName);
Console.WriteLine(string.Format("Creation time: {0}", theFile.CreationTime.ToString()));
Console.WriteLine(string.Format("Size: {0} bytes", theFile.Length.ToString()));
}

下面的代碼分別使用了File類和FileInfo類來示範如何刪除檔案 複製代碼 代碼如下:void DeleteFile1()
{
string fileToBeDeleted = @"c:\temp\New Text~ Document (3).txt";
if (File.Exists(fileToBeDeleted))
{
File.Delete(fileToBeDeleted);
}
}
void DeleteFile2()
{
string fileToBeDeleted = @"c:\temp\New Text~ Document (3).txt";
FileInfo file = new FileInfo(fileToBeDeleted);
if (file.Exists)
{
file.Delete();
}
}

下面的代碼分別使用了Directory類和DirectoryInfo類來示範如何刪除檔案夾 複製代碼 代碼如下:void DeleteFolder1()
{
string folderToBeDeleted = @"c:\temp\test";
if (Directory.Exists(folderToBeDeleted))
{
// true is recursive delete:
Directory.Delete(folderToBeDeleted, true);
}
}

void DeleteFolder2()
{
string folderToBeDeleted = @"c:\temp\test";
DirectoryInfo folder = new DirectoryInfo(folderToBeDeleted);
if (folder.Exists)
{
folder.Delete(true);
}
}

下面的代碼分別使用了File類和FileInfo類來示範如何移動檔案 複製代碼 代碼如下:void MoveFile1()
{
string fileToMove = @"c:\temp\New Text Document.txt";
string fileNewDestination = @"c:\temp\test.txt";
if (File.Exists(fileToMove) && !File.Exists(fileNewDestination))
{
File.Move(fileToMove, fileNewDestination);
}
}

void MoveFile2()
{
string fileToMove = @"c:\temp\New Text Document.txt";
string fileNewDestination = @"c:\temp\test.txt";
FileInfo file = new FileInfo(fileToMove);
if (file.Exists)
{
file.MoveTo(fileNewDestination);
}
}

下面的代碼分別使用了Directory類和DirectoryInfo類來示範如何移動檔案夾 複製代碼 代碼如下:void MoveFolder1()
{
string folderToMove = @"c:\temp\test";
string folderNewDestination = @"c:\temp\test2";
if (Directory.Exists(folderToMove))
{
Directory.Move(folderToMove, folderNewDestination);
}
}

void MoveFolder2()
{
string folderToMove = @"c:\temp\test";
string folderNewDestination = @"c:\temp\test2";
DirectoryInfo folder = new DirectoryInfo(folderToMove);
if (folder.Exists)
{
folder.MoveTo(folderNewDestination);
}
}

下面的代碼分別使用了File類和FileInfo類來示範如何複製檔案 複製代碼 代碼如下:void CopyFile1()
{
string sourceFile = @"c:\temp\New Text Document.txt";
string destinationFile = @"c:\temp\test.txt";
if (File.Exists(sourceFile))
{
// true is overwrite
File.Copy(sourceFile, destinationFile, true);
}
}

void CopyFile2()
{
string sourceFile = @"c:\temp\New Text Document.txt";
string destinationFile = @"c:\temp\test.txt";
FileInfo file = new FileInfo(sourceFile);
if (file.Exists)
{
// true is overwrite
file.CopyTo(destinationFile, true);
}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.