Click here to Skip to main content
15,914,608 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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.

C#
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)
        {

        }
    }
}
Posted
Updated 8-Jan-16 21:44pm
v2
Comments
BillWoodruff 9-Jan-16 4:37am    
What is "funny" about a program that deletes files ?

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
 
Share this answer
 
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[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900