C#对文件及文件夹的操作包括删除、移动与删除

在.Net中,对文件和文件夹的操作可以使用File类和Directory类,也可以使用FileInfo类和DirectoryInfo类。

File类和Directory类都是静态类。使用它们的好处是不需要初始化对象。如果你对某一个文件或文件夹只进行一次操作,那你最好使用该静态类的静态方法,比如File.Move,File.Delete等等。(注:在File.Move()和File.Delete()时,如果使用了streamreader在需要先streamreader.close())。如果你需要对一个文件或文件夹进行多次操作,那最好还是使用FileInfo和DirectoryInfo类。因为File类和Directory是静态类,所以你每次对一个文件或文件夹进行操作之前,它们都需要对该文件或文件夹进行一些检查。

 

1、下面的这段代码演示了如何获得文件夹的信息,包括获得文件夹下的子文件夹,以及文件夹下的文件。这里使用了DirectoryInfo 类来完成,当然你也可以使用Directory静态类。

    void DisplayFolder()
    {
        string folderFullName = @"c:\temp";
        DirectoryInfo theFolder = new DirectoryInfo(folderFullName);
        //是否存在改文件
        if (!theFolder.Exists)
            throw new DirectoryNotFoundException("Folder not found: " + folderFullName);

        //theFolder.GetDirectories():得到给文件夹中的所有子文件夹
        foreach (DirectoryInfo subFolder in theFolder.GetDirectories())
        {
            Console.WriteLine(subFolder.Name);
        }
        //theFolder.GetFiles() : 得到文件夹中所有的子文件
        foreach (FileInfo file in theFolder.GetFiles())
        {
            Console.WriteLine(file.Name);
        }
    }

2、下面演示了如何使用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);
    }

3、下面的代码分别使用了File类和FileInfo类来演示如何删除文件。

    //如果 使用了 streamreader 在删除前 必须先关闭流 ,否则无法删除 streamreader.close();
    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();
        }
    }

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

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

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

    //如果 使用了 streamreader 在移动前 必须先关闭流 ,否则无法移动 streamreader.close();
    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);
        }
    }

6、下面的代码分别使用了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);
         }
}

7、下面的代码分别使用了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);
         }
}

 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值