在.Net中,對(duì)文件(File)和文件夾(Folder)的操作可以使用File類和Directory類,也可以使用FileInfo類和DirectoryInfo類。文件夾(Folder)是只在Windows操作系統(tǒng)中使用的名詞。在操作系統(tǒng)的理論中,人們更習(xí)慣于使用目錄(Directory)這個(gè)名詞。或許微軟為了有朝一日將.Net移植到其他的操作系統(tǒng)中(實(shí)際上也有很多人也在做著這個(gè)項(xiàng)目),所以還是以Directory來(lái)命名操作文件夾的類。
File類和Directory類都是靜態(tài)類。使用它們的好處是不需要初始化對(duì)象。如果你對(duì)某一個(gè)文件或文件夾只進(jìn)行一次操作,那你最好使用該靜態(tài)類的靜態(tài)方法,比如File.Move,F(xiàn)ile.Delete等等。如果你需要對(duì)一個(gè)文件或文件夾進(jìn)行多次操作,那最好還是使用FileInfo和DirectoryInfo類。因?yàn)镕ile類和Directory是靜態(tài)類,所以你每次對(duì)一個(gè)文件或文件夾進(jìn)行操作之前,它們都需要對(duì)該文件或文件夾進(jìn)行一些檢查,比如authentication。如果使用FileInfo類和DirectoryInfo類,只在初始化類的對(duì)象時(shí)進(jìn)行相關(guān)的檢查工作,也就是說(shuō)只需要做一次,所以如果你需要對(duì)某個(gè)文件或文件夾進(jìn)行多次操作,那最好使用FileInfo類和DirectoryInfo類。
下面的這段代碼演示了如何獲得文件夾的信息,包括獲得文件夾下的子文件夾,以及文件夾下的文件。這里使用了DirectoryInfo 類來(lái)完成,當(dāng)然你也可以使用Directory靜態(tài)類。
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類來(lái)獲得文件的相關(guān)信息,包括文件的創(chuàng)建日期,文件的大小等等。當(dāng)然你同樣也可以使用File靜態(tài)類來(lái)完成。
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類來(lái)演示如何刪除文件
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類來(lái)演示如何刪除文件夾
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類來(lái)演示如何移動(dòng)文件
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類來(lái)演示如何移動(dòng)文件夾
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類來(lái)演示如何復(fù)制文件
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);
}
}