修改此代码需要帮助

时间:2021-07-08 21:35:44

I want to delete all .rar extension file from all directory with particular drive. Say i have "D:\Test" under this i have created many .rar files or .zip files. Once the program run only all .rar file should be deleted and other extension files should remain same and it should display that how many no of rar files have been deleted. I can create n of files in n detpht of subdirectory but while program run all .rar files should be deleted. For this i have written a program, and i have created many files in that particular drive, but when i am running the application saying that there is no files i mean its always checking the else condition. Here is my code pls somebody modify it.

我想从特定驱动器的所有目录中删除所有.rar扩展文件。假设我有“D:\ Test”,我创建了许多.rar文件或.zip文件。一旦程序运行,只应删除所有.rar文件,其他扩展文件应保持相同,并且应显示已删除多少个rar文件。我可以在n detpht子目录中创建n个文件,但是在程序运行时,应该删除所有.rar文件。为此,我编写了一个程序,并且我在该特定驱动器中创建了许多文件,但是当我运行应用程序时说没有文件我的意思是它总是检查其他条件。这是我的代码请有人修改它。

static void Main(string[] args)
{
    DirectoryInfo dirMain = new DirectoryInfo("D:\\Test");
    if (dirMain != null)
    {
        FileInfo[] dirRar = dirMain.GetFiles("*.rar", SearchOption.AllDirectories);
        if (dirRar != null && dirRar.Length > 0)
        {
            for (int i = 0; i < dirRar.Length; i++)
            {
                Console.WriteLine(dirRar[i].FullName);
                dirRar[i].Delete();
            }
            Console.WriteLine("Total no of files deleted" + dirRar.Length.ToString());
        }
        else
        {
            Console.WriteLine("There is no file");
        }
    }
    Console.ReadKey();
}

4 个解决方案

#1


Works fine for me; maybe check that d:\Test exists? As an aside - note that the recursive overload of GetFiles is a bit flaky when it gets to complex permission sets. You would do better by doing the recursion manually, using try/catch around sub-folders:

对我来说很好;也许检查d:\ Test是否存在?另外请注意,GetFiles的递归重载在获得复杂的权限集时有点不稳定。使用try / catch周围子文件夹手动执行递归会做得更好:

int count = 0;
Queue<string> dirs = new Queue<string>();
dirs.Enqueue(@"d:\Test");
while(dirs.Count > 0) {
    string dir = dirs.Dequeue();
    try
    {
        foreach (string subdir in Directory.GetDirectories(dir))
        {
            dirs.Enqueue(subdir);
        }
    }
    catch (Exception ex) { Console.Error.WriteLine(ex); }// access denied etc

    foreach (string file in Directory.GetFiles(dir, "*.rar"))
    {
        try
        {
            File.Delete(file);
            count++;
        }
        catch (Exception ex) { Console.Error.WriteLine(ex); }// access denied etc
    }
}
Console.WriteLine("Deleted: " + count);

#2


You code looks fine. If you are entering the else condition it means that there are either no .rar files under d:\test (or any subfolders) or that the account you are running your code under doesn't have read permissions to this folder.

你的代码看起来很好。如果您输入else条件,则表示d:\ test(或任何子文件夹)下没有.rar文件,或者您运行代码的帐户没有此文件夹的读取权限。

#3


Few suggestions:

  1. Check that the files or directory is not marked as hidden.
  2. 检查文件或目录是否未标记为隐藏。

  3. Try FileInfo[] dirRar = dirMain.GetFiles("*.*"); Iterate on each file and check whether you are getting your rar file
  4. 尝试FileInfo [] dirRar = dirMain.GetFiles(“*。*”);迭代每个文件并检查是否收到rar文件

#4


It works here.

它在这里工作。

All rar-files in D:\Test was deleted.

D:\ Test中的所有rar文件都已删除。

#1


Works fine for me; maybe check that d:\Test exists? As an aside - note that the recursive overload of GetFiles is a bit flaky when it gets to complex permission sets. You would do better by doing the recursion manually, using try/catch around sub-folders:

对我来说很好;也许检查d:\ Test是否存在?另外请注意,GetFiles的递归重载在获得复杂的权限集时有点不稳定。使用try / catch周围子文件夹手动执行递归会做得更好:

int count = 0;
Queue<string> dirs = new Queue<string>();
dirs.Enqueue(@"d:\Test");
while(dirs.Count > 0) {
    string dir = dirs.Dequeue();
    try
    {
        foreach (string subdir in Directory.GetDirectories(dir))
        {
            dirs.Enqueue(subdir);
        }
    }
    catch (Exception ex) { Console.Error.WriteLine(ex); }// access denied etc

    foreach (string file in Directory.GetFiles(dir, "*.rar"))
    {
        try
        {
            File.Delete(file);
            count++;
        }
        catch (Exception ex) { Console.Error.WriteLine(ex); }// access denied etc
    }
}
Console.WriteLine("Deleted: " + count);

#2


You code looks fine. If you are entering the else condition it means that there are either no .rar files under d:\test (or any subfolders) or that the account you are running your code under doesn't have read permissions to this folder.

你的代码看起来很好。如果您输入else条件,则表示d:\ test(或任何子文件夹)下没有.rar文件,或者您运行代码的帐户没有此文件夹的读取权限。

#3


Few suggestions:

  1. Check that the files or directory is not marked as hidden.
  2. 检查文件或目录是否未标记为隐藏。

  3. Try FileInfo[] dirRar = dirMain.GetFiles("*.*"); Iterate on each file and check whether you are getting your rar file
  4. 尝试FileInfo [] dirRar = dirMain.GetFiles(“*。*”);迭代每个文件并检查是否收到rar文件

#4


It works here.

它在这里工作。

All rar-files in D:\Test was deleted.

D:\ Test中的所有rar文件都已删除。