搜索和删除程序c#程序无法正常工作 [英] Search and delete program c# Program not working

查看:82
本文介绍了搜索和删除程序c#程序无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图制作一个有趣的程序,从计算机中搜索和删除mp3文件。我面临的两个问题是: -



1)我不知道如何自动查找扫描文件的所有分区。



2)当前程序正在扫描给定文件夹但不删除文件。



I tried to make a funny program that search and delete mp3 files from computer. The 2 problems i faced are :-

1) I don't know how to automatically find all partition for scanning files.

2)The current program is scanning the given folder but not deleting files.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace prgtest
{
   
    public partial class Form1 : Form
    {
      
        public Form1()
        {

            InitializeComponent();
            
        }

        private void button1_Click(object sender, EventArgs e)
        {

            foreach (string dir in Directory.GetDirectories(@"I:\Mp3 Delete\Mama\Music"))
            {
                try
                {
                    
            foreach(string file in Directory.GetFiles(dir,"*.MP3"))
            {
                textBox1.Text = file;
                textBox2.Text = dir;
            //string dir1=Path.GetFileName(file);
            //string mp3=Path.GetFileName(file);
            File.Delete(file);
            }
                }
                catch
                {
                    textBox1.Text = "Error";
                }
            }
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

推荐答案

您不需要查找所有分区。首先,并非所有分区都有文件系统。相反,您需要找到所有逻辑驱动器。具体方法如下: DriveInfo.GetDrives Method(System .IO) [ ^ ]。



而不是方法 Directory.GetFiles ,您可以使用另一个 Directory.GetFiles 方法,该方法以递归方式查找文件: Directory.GetFiles方法(String,String,SearchOption)(System.IO) [ ^ ]。



您的代码有问题,因此它可能是文件未被删除甚至找不到的原因之一。最糟糕的是异常处理。您阻止异常传播,并仅显示一些错误,这不是异常信息。你只需隐藏异常。



只需准确编写代码,在调试器下运行它以查看它是如何工作的,并消除错误,测试它。



-SA
You don't need to find all partitions. First of all, not all partitions have file systems. Instead, you need to find all logical drives. This is how: DriveInfo.GetDrives Method (System.IO)[^].

Instead of the method Directory.GetFiles, you can use another Directory.GetFiles method which finds files recursively: Directory.GetFiles Method (String, String, SearchOption) (System.IO)[^].

Your code is buggy, so it can be one of the reasons why the file is not deleted, or even not found. The worst thing is exception handling. You block exception propagation, and only show the some Error, which is not exception information. You simply hide exceptions.

Just write your code accurately, run it under the debugger to see how it works and to eliminate bugs, test it well.

—SA


你可以用 DriveInfo.GetDrives Method(System.IO) [ ^ ]它将返回列表DriveInfo

然后你可以使用搜索每个驱动器DriveInfo.RootDirectory.FullName

check c# - 如何在所有驱动器中搜索.txt文件的所有目录? - 堆栈溢出 [ ^ ]
you can use DriveInfo.GetDrives Method (System.IO)[^] and it will return list of DriveInfo
then you can search on each drive using DriveInfo.RootDirectory.FullName
check c# - How to search all directories in all drives for .txt files? - Stack Overflow[^]


这篇关于搜索和删除程序c#程序无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆