分享

C# 对文件与文件夹的操作 -- 删除、移动与复制

 康and豪图书馆 2013-10-08

在.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); 

    } 

}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多