Q:

Skip "$RECYCLE.BIN" when copying all directories

I am trying to copy all folders to c: the issue is it keeps trying to access the recycle bin folder. I tried many thing to avoid it but it did't work. I had same issue with system volume information but i simply skipped over it. I don't know why this approach is not working with the $recycle.bin
public void Copy(string sourceDirectory, string targetDirectory)
{
    DirectoryInfo diSource = new DirectoryInfo(sourceDirectory);
    DirectoryInfo diTarget = new DirectoryInfo(targetDirectory);
    string[] entries;
    try
    {
        //Gets list of all files and directories (we need it for progress bar)
        entries = Directory.GetFileSystemEntries(sourceDirectory, "*", SearchOption.AllDirectories);
        //entries = Directory.GetFiles(sourceDirectory, "*.*", SearchOption.AllDirectories)
        //    .Where(d => !d.StartsWith("$RECYCLE.BIN"))
        //    .Where(d => !d.StartsWith("System Volume Information")).ToArray();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
        return;
    }

    Invoke(new Action(() => progressBar1.Maximum = entries.Count()));
    Invoke(new Action(() => progressBar1.Step = 1));
    Invoke(new Action(() => progressBar1.Value = 0)); //Using Invoke to prevent Cross thread exception
    CopyAll(diSource, diTarget, entries);
}

public void CopyAll(DirectoryInfo source, DirectoryInfo target, string[] entries)
{
    //    lblInfo.Text = "Copying " + source;

    Directory.CreateDirectory(target.FullName);

    // Copy each file into the new directory.

    foreach (FileInfo fi in source.GetFiles())
    {
        try
        {
            if (source.ToString() != "D:\\$RECYCLE.BIN" && source.ToString() != "System Volume Information")
            {
                if (!IsWorking)

                {
                    Invoke(new Action(() => lblInfo.Text = "Stopped"));
                    return;
                }

                //Using Invoke to prevent Cross thread exception
                Invoke(new Action(() => this.lblInfo.Text = string.Format("Copied {0}\\{1}", source.FullName, fi.Name)));

                if (File.Exists(Path.Combine(target.FullName, fi.Name)))
                {
                    File.Delete(Path.Combine(target.FullName, fi.Name));
                }
                fi.CopyTo(Path.Combine(target.FullName, fi.Name), true);
                Application.DoEvents();

                Invoke(new Action(() => progressBar1.Value++));
            }
        }
        catch (UnauthorizedAccessException ex)
        {
            // ok, so we are not allowed to dig into that directory. Move on.
        }

    }
    // Copy each subdirectory using recursion.

    foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
    {
        try
        {
            if (diSourceSubDir.ToString() != "System Volume Information" && diSourceSubDir.ToString() != "$RECYCLE.BIN")
            {
                DirectoryInfo nextTargetSubDir =
                    target.CreateSubdirectory(diSourceSubDir.Name);
                CopyAll(diSourceSubDir, nextTargetSubDir, entries);
                Invoke(new Action(() => progressBar1.Value++));
            }

            else
            {
                Invoke(new Action(() => progressBar1.Value = progressBar1.Value + 2));
            }
        }
        catch (UnauthorizedAccessException ex)
        {
            // ok, so we are not allowed to dig into that directory. Move on.
        }
    }

I tried the commented out LINQ and if statements and most online answers. I don't want to run it as admin and I deleted the app.manifest file I appreciate the help.

A:

Here is what I did: I added the conditions and removed the return; and show box so when it catch something it just moves on to the next directory.  
try
{
    if (!diSource.ToString().Contains("System Volume Information") && 
      !diSource.ToString().ToUpper().Contains("$RECYCLE.BIN"))
    {

        entries = Directory.GetFileSystemEntries(sourceDirectory, "*", 
        SearchOption.AllDirectories);

    }
}

 catch (UnauthorizedAccessException ex)
 {
    //ok, so we are not allowed to dig into that directory. Move on.
 }

