用C#.NET实现删除指定文件

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace DeleteFile
{
    class Program
    {
      public static void DeleteFile(string dirRoot)
      {
        string deleteFileName = "_desktop.ini";//要删除的文件名称
        try
        {
          string[] rootDirs = Directory.GetDirectories(dirRoot); //当前目录的子目录:
          string[] rootFiles = Directory.GetFiles(dirRoot);        //当前目录下的文件:

          foreach (string s2 in rootFiles)
          {
            if (s2.Contains(deleteFileName))
            {
              Console.WriteLine(s2);
              File.Delete(s2);                      //删除文件
            }
          }
          foreach (string s1 in rootDirs)
          {
            DeleteFile(s1);
          }
        }
        catch(Exception ex)
        {
          Console.WriteLine(ex.Message.ToString());
        }

      }
      static void Main(string[] args)
      {
        string[] drives = Directory.GetLogicalDrives();//系统所有根目录

        foreach (string s in drives)
        {
          DeleteFile(s);
        }
        Console.WriteLine("Delete Files Complete!");
        Console.ReadLine();
      }
    }
}

你可能感兴趣的:(exception,String,delete,Class)