I want to make a program that checks if any file is null, but is throwing an exception, what's wrong with 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();
}