none
How can I handle/skip not found path exception ? RRS feed

  • Question

  • int numberofdirs = 0;
                void DirSearch(string rootDirectory, string filesExtension, string[] textToSearch, BackgroundWorker worker, DoWorkEventArgs e)
                {
                    List<string> resultsoftextfound = new List<string>();
                    List<string> resultsoftextfound1 = new List<string>();
                    List<string> filePathList = new List<string>();
                    int numberoffiles = 0;
                    try
                    {
                        filePathList = SearchAccessibleFilesNoDistinct(rootDirectory, null,worker,e).ToList();
                    }
                    catch (Exception err)
                    {
                        string ad = err.ToString();
                    }

    The exception is in the catch :

    "Could not find a part of the path 

    And the SearchAccessibleFilesNoDistinct code :

    string restrictedFile = "";
                List<string> restrictedFiles = new List<string>();
                int numberofrestrictedFiles = 0;
                int numberoffiles = 0;
                IEnumerable<string> SearchAccessibleFilesNoDistinct(string root, List<string> files,BackgroundWorker worker, DoWorkEventArgs e)
                {
                    _busy.WaitOne();
                    if (files == null)
                        files = new List<string>();
                    if (Directory.Exists(root))
                    {
                        foreach (var file in Directory.EnumerateFiles(root))
                        {
                            if (worker.CancellationPending == true)
                            {
                                e.Cancel = true;
                                return files;
                            }
                            restrictedFile = file;
                            string ext = Path.GetExtension(file);
                            if (!files.Contains(file) && ext == textBox2.Text)
                            {
                                files.Add(file);
                            }
                            numberoffiles++;
                            label24.Invoke((MethodInvoker)delegate
                            {
                                label24.Text = numberoffiles.ToString();
                                label24.Visible = true;
                            });
                        }
                        foreach (var subDir in Directory.EnumerateDirectories(root))
                        {
                            if (worker.CancellationPending == true)
                            {
                                e.Cancel = true;
                                return files;
                            }
                            try
                            {
                                SearchAccessibleFilesNoDistinct(subDir, files,worker, e);
                                //files.Add(subDir);
                            }
                            catch (UnauthorizedAccessException)
                            {
                                restrictedFiles.Add(restrictedFile);
                                numberofrestrictedFiles++;
                                label11.Invoke((MethodInvoker)delegate
                                {
                                    label11.Text = numberofrestrictedFiles.ToString();
                                    label11.Visible = true;
                                });
                                continue;
                            }
                        }
                    }
                    return files;
                }

    I want to skip it and continue and not to stop the whole process if a path/file not found.




    Sunday, May 24, 2020 1:52 PM

Answers

  • Hi Chocolade1972,
    Based on the connection you provided, I didn't reproduce your problem successfully .
    In general, you will try to catch your exceptions as specific as possible. But especially if you are catching the exceptions without actually using them to throw an error, you might as well catch a group of exceptions. Even then you will try to make it as specific as possible.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Monday, May 25, 2020 9:54 AM

All replies

  • I found now that when the none exist path is getting to this line :

    foreach (var file in Directory.EnumerateFiles(root))

    Inside the SearchAccessibleFilesNoDistinct function it's never continue. Then how can I handle/skip the not exist/found path at this point so it will continue the process instead stopping ?

    Sunday, May 24, 2020 3:11 PM
  • My whole project can be find in this link I uploaded the project some time ago to csharp corner site :

    https://www.c-sharpcorner.com/code/3134/search-for-text-in-files.aspx

    It will be easier to see the problem or to reproduce the problem instead to upload the whole code to my question here. 

    Sunday, May 24, 2020 9:57 PM
  • Hi Chocolade1972,
    Based on the connection you provided, I didn't reproduce your problem successfully .
    In general, you will try to catch your exceptions as specific as possible. But especially if you are catching the exceptions without actually using them to throw an error, you might as well catch a group of exceptions. Even then you will try to make it as specific as possible.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Monday, May 25, 2020 9:54 AM