Quote:
I have tried using try catch , i get error the name fs_infos does not exist in current ent context
The only way to "skip an exception" is to use
try...catch
, but the chances are you are using it incorrectly.
At a guess, you put the block around the first line only:
try
{
FileInfo[] fs_infos = dir_info.GetFiles(pattern);
}
catch (...)
{
...
}
foreach (FileInfo fs_info in fs_infos)
{
Which creates it's own scope, so
fs_infos
is only available inside the
try...catch
block.
You need to put it round the whole thing if the IO error comes when you call GetFiles:
try
{
FileInfo[] fs_infos = dir_info.GetFiles(pattern);
foreach (FileInfo fs_info in fs_infos)
{
...
}
}
catch (...)
{
...
}
Because GetFiles will not return any values if it throws an exception.
If that isn't the line that throws the exception, and you want to skip files that do, you need a second
try...catch
block inside the loop as well:
try
{
FileInfo[] fs_infos = dir_info.GetFiles(pattern);
foreach (FileInfo fs_info in fs_infos)
{
try
{
...
}
catch (...)
{
...
}
}
}
catch (...)
{
...
}