zoukankan      html  css  js  c++  java
  • C# 对文件与文件夹的操作包括删除、移动与复制

    在.Net中,对文件(File)和文件夹(Folder)的操作可以使用File类和Directory类,也可以使用FileInfo类和DirectoryInfo类,本文将详细介绍,需要的朋友可以参考。

    在.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:	emp";
             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:	emp";
             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:	empNew Text~ Document (3).txt";
             if (File.Exists(fileToBeDeleted)) {
                File.Delete(fileToBeDeleted);
             }
          }
          void DeleteFile2() {
             string fileToBeDeleted = @"c:	empNew Text~ Document (3).txt";
             FileInfo file = new FileInfo(fileToBeDeleted);
             if (file.Exists) {
                file.Delete();
             }
          }

    下面的代码分别使用了Directory类和DirectoryInfo类来演示如何删除文件夹。

    代码如下:

          void DeleteFolder1() {
             string folderToBeDeleted = @"c:	emp	est";
             if (Directory.Exists(folderToBeDeleted)) {
                // true is recursive delete: 
                Directory.Delete(folderToBeDeleted, true);
             }
          }
          void DeleteFolder2() {
             string folderToBeDeleted = @"c:	emp	est";
             DirectoryInfo folder = new DirectoryInfo(folderToBeDeleted);
             if (folder.Exists) {
                folder.Delete(true);
             }
          }

    下面的代码分别使用了File类和FileInfo类来演示如何移动文件。

    代码如下:

          void MoveFile1() {
             string fileToMove = @"c:	empNew Text Document.txt";
             string fileNewDestination = @"c:	emp	est.txt";
             if (File.Exists(fileToMove) && !File.Exists(fileNewDestination)) {
                File.Move(fileToMove, fileNewDestination);
             }
          }
          void MoveFile2() {
             string fileToMove = @"c:	empNew Text Document.txt";
             string fileNewDestination = @"c:	emp	est.txt";
             FileInfo file = new FileInfo(fileToMove);
             if (file.Exists) {
                file.MoveTo(fileNewDestination);
             }
          }

    下面的代码分别使用了Directory类和DirectoryInfo类来演示如何移动文件夹。

    代码如下:

          void MoveFolder1() {
             string folderToMove = @"c:	emp	est";
             string folderNewDestination = @"c:	emp	est2";
             if (Directory.Exists(folderToMove)) {
                Directory.Move(folderToMove, folderNewDestination);
             }
          }
          void MoveFolder2() {
             string folderToMove = @"c:	emp	est";
             string folderNewDestination = @"c:	emp	est2";
             DirectoryInfo folder = new DirectoryInfo(folderToMove);
             if (folder.Exists) {
                folder.MoveTo(folderNewDestination);
             }
          }

    下面的代码分别使用了File类和FileInfo类来演示如何复制文件。

    代码如下:

          void CopyFile1() {
             string sourceFile = @"c:	empNew Text Document.txt";
             string destinationFile = @"c:	emp	est.txt";
             if (File.Exists(sourceFile)) {
                // true is overwrite 
                File.Copy(sourceFile, destinationFile, true);
             }
          }
          void CopyFile2() {
             string sourceFile = @"c:	empNew Text Document.txt";
             string destinationFile = @"c:	emp	est.txt";
             FileInfo file = new FileInfo(sourceFile);
             if (file.Exists) {
                // true is overwrite 
                file.CopyTo(destinationFile, true);
             }
          }
  • 相关阅读:
    Nulls first和nulls last
    json.parse()和json.stringify()
    将单个的.java文件通过javac编辑为.class文件
    看别人项目思路:
    我想成为怎样的人?
    装逼语录:
    Uncompressing Linux... done, booting the kernel
    linux 内核模块最小环境编译
    select 定时器
    mount
  • 原文地址:https://www.cnblogs.com/zpx1986/p/5584619.html
Copyright © 2011-2022 走看看