So let's say as example i have the image path which is "D:\..." anything. file.FullName is this path, but sometimes when i put that path into the array the element is null but i have no idea why.
Let's say i have 100 pictures in the folder then 10 percent of their names are null in the array but i checked in debuggin file.FullName is never null.
Someone has an idea why this happens? Or did i overlook something?
int z = 0;
foreach (FileInfo file in Variables.dir.GetFiles())
{
try
{
this.myImageList.Images.Add(Image.FromFile(file.FullName));
names[z] = file.FullName;
}
catch
{
Console.WriteLine("This is not an image file");
}
z++;
}
Your code, even when erroring, is still incrementing z
. Move z
to within try{}
instead of outside the catch{}
/ finally{}
. This will cause z
to only increment when the program finds only files that it is looking for / that match your criteria (i.e Image files).
int z = 0;
foreach (FileInfo file in Variables.dir.GetFiles())
{
try
{
this.myImageList.Images.Add(Image.FromFile(file.FullName));
names[z] = file.FullName;
z++;
}
catch
{
Console.WriteLine("This is not an image file");
}
}
Your program could be finding hidden files or others when not looking in a debugged file path. Multiple reasons for your code to catch{}
, but not certain without adding in:
try{ ... }
catch(Exception ex)
{
Console.WriteLine("This is not an image file: \n" + ex);
}
in order to at least get the proper error to further debug