Click here to Skip to main content
15,908,834 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
When I run this code :
FileInfo[] fs_infos = dir_info.GetFiles(pattern);//error here

            foreach (FileInfo fs_info in fs_infos)
            {
                if (target.Length == 0)
                {
                    listBox1.Items.Add(fs_info.FullName);
                }
                else
                {
                    string txt = File.ReadAllText(fs_info.FullName);

                    if (txt.IndexOf(target, StringComparison.OrdinalIgnoreCase) >= 0)
                    {
                        listBox1.Items.Add(fs_info.FullName);

                    }


                }
            }

i got IOexception.
I want to skip this file and get other files.How can I do?

What I have tried:

I have tried using try catch , i get error the name fs_infos does not exist in current ent context
Posted
Updated 6-Aug-20 17:25pm
v2

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:
C#
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:
C#
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:
C#
try
   {
   FileInfo[] fs_infos = dir_info.GetFiles(pattern);
   foreach (FileInfo fs_info in fs_infos)
      {
      try
         {
         ...
         }
      catch (...)
         {
         ...
         }
      }
   }
catch (...)
   {
   ...
   }
 
Share this answer
 
Comments
Lê Hiển Vinh 8-Jul-19 4:34am    
Thank you very much!
Your solution is very helpful
OriginalGriff 8-Jul-19 4:46am    
You're welcome!
BillWoodruff 8-Jul-19 4:48am    
+5 Upvoted to counteract unwarranted downvote.
BillWoodruff 8-Jul-19 5:50am    
did I hallucinate there was a down-vote :)
OriginalGriff 8-Jul-19 5:57am    
I don;t think so - maybe the OP revised it?
I advise you to never ignore a Exception: when you catch an Exception, re-throw it.

Consider this example:
string path = @"C:\Users\test_user\Desktop\Test";
FileInfo[] fs_infos;
string target = "";

// Directory must exist !
if (!Directory.Exists(path))
{
    throw new DirectoryNotFoundException(@"no directory named: {path}");
}

DirectoryInfo dir = new DirectoryInfo(path);

// several types  of errors might occur here:
// UnauthorizedAccessException 
// InvalidOperationException
// etc.
try
{
    fs_infos = dir.GetFiles();
}
catch (Exception exception)
{
    // put a break-point here and examine the error
    Console.WriteLine(exception);
    throw;
}

// any files ?
if (fs_infos.Length == 0)
{
    throw new FileNotFoundException(@"no files in: {path}");
}

// now you can process files
Consider using NET >= 4 EnumerateFiles: [^]

Consider data-binding your ListBox Control to a Collection of FileInfo:
BindingSource bs = new BindingSource();
bs.DataSource = fs_infos;

listBox1.DisplayMember = "Name"
listBox1.DisplayMember = "FullName";

listBox1.DataSource = bs;
Then: the FileName (not the full file path) will show in the ListBox, and in the 'SelectedValueChanged EventHandler the 'SelectedValue will be the full file path.
 
Share this answer
 
v3
Comments
Lê Hiển Vinh 8-Jul-19 4:34am    
Thank you very much because your advise
BillWoodruff 8-Jul-19 5:49am    
You're welcome; please note I made a change to the binding example at the end: you should always set the 'DataSource property AFTER setting 'Display and 'Value members.
Use invoke method to ignore the exception or you can try
SpecificException
try this code
try
{
   // code
}
catch (SpecificException)
{ }


Useful links for you

C# Best way to ignore exception - Stack Overflow[^]
Ignore Exception in C# - Stack Overflow[^]
Explicitly Ignoring Exceptions in C# - Rick Strahl's Web Log[^]
 
Share this answer
 
Comments
Lê Hiển Vinh 8-Jul-19 4:34am    
Thank you very much!
Use continue in catch section
try
{
   // code
}
catch (SpecificException)
{
continue;
}
 
Share this answer
 
v2

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