What’s wrong with my code?

Asked

Viewed 329 times

0

I want to make a program that checks if any file is null but this casting an exception, which is wrong in my code?:

    static List<string> GetFiles(string path)
    {

        string[] directories = Directory.GetDirectories(path);

        List<string> filesList = new List<string>();

        foreach(string directory in directories)
        {
            string[] files = Directory.GetFiles(directory);

            foreach(string f in files)
            {
                filesList.Add(directories + "\\" + f);
            }
        }
        return filesList;
    }

    private void txtB_Path_TextChanged(object sender, EventArgs e) {  }

    private void bt_Start_Click(object sender, EventArgs e)
    {
        List<string> files = GetFiles(txtB_Path.Text);

        List<string> nullFiles = new List<string>();

        List<string> nullFilesDirectories = new List<string>();

        try
        {
            foreach (string item in files)
            {

                byte[] fileBytes = File.ReadAllBytes(item);

                foreach (byte bytes in fileBytes)
                {
                    if (bytes == 0)
                    {
                        nullFiles.Add(fileBytes.ToString());
                        nullFilesDirectories.Add(item);
                    }
                }
            }
            lb_NullFiles.Text = nullFiles.Count.ToString();

        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
            lb_NullFiles.Text = "0";
        }

        try {
            if (chckB_DeleteNullFiles.Checked)
            {
                foreach (string file in nullFilesDirectories)
                {
                    File.Delete(file);
                }
            }
        }
        catch
        {
            MessageBox.Show("Could not delete files");
        }
        lb_Files.Text = files.Count.ToString();

    }
  • 2

    You can speak in Portuguese if this is your "mother language";) Our official language is Portuguese.

  • ok thanks guy ! (Agr q vi q tava no portugues kkkk)

  • Forgot to translate the title.

  • 1

    Thank you, I’ve already edited and I’m going to do the tour

  • 1

    Lines 48-70 Exception: "No support for the given path format"

  • 1

    Instead of commenting on things relevant to the question, include it in the question itself. It will become clearer.

  • Ss, I’m new here, qlqr tip q have to make it clear...

Show 2 more comments

2 answers

1


Perhaps there is confusion there as to whether a file is null. I understand that it is not possible, the file may be empty / blank / etc...

Whereas you want to just search for the files in a folder, and delete all the files that are empty, ie size = 0, you can use the following code:

bool delArquivosVazios = true; //valor do seu checkbox
string[] files = Directory.GetFiles("c:\\pasta\\", "*.*", SearchOption.AllDirectories);
foreach (string file in files)
{
    FileInfo obj = new FileInfo(file);
    if (obj.Length == 0 && delArquivosVazios)
        obj.Delete();
}

You can still specify a filter:

Change "*.*" for "*.txt" to search only text files, for example.

You can also change the search option:

SearchOption.AllDirectories To the informed folder and all its subdirectories

SearchOption.TopDirectoryOnly For only the informed folder.

0

You are adding to the list of null files the contents of the file, instead of its path.

Also, I believe you don’t need that internal loop reading every byte of the file... As it is, it is quite possible that all files in the directory are deleted because they can contain a byte '0'.

I believe that would be your intention :

try
{
    foreach (string item in files)
    {
        byte[] fileBytes = File.ReadAllBytes(item);

        if (string.IsNullOrWhitespace(fileBytes.ToString())
        {    
             nullFiles.Add(fileBytes.ToString);
             lb_NullFiles.Text = nullFiles.Count.ToString();

        }
   } 
   catch(Exception ex)
   {
       MessageBox.Show(ex.Message);
       lb_NullFiles.Text = "0";
   }
} 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.