Skip to main content
added 8 characters in body
Source Link
Heslacher
  • 50.4k
  • 5
  • 81
  • 175

I need to get a list of all files supported by some libary (images) in a specified directory and all subderictories. Root directory of drive might be chosen and in this case I want to skip system folders like "Windows" and "AppData" (some other?).

This code looks resonablereasonable to me. But I want to make sure that it is done right.

I need to get list of all files supported by some libary (images) in specified directory and all subderictories. Root directory of drive might be chosen and in this case I want to skip system folders like "Windows" and "AppData" (some other?).

This code looks resonable to me. But I want to make sure that it done right.

I need to get a list of all files supported by some libary (images) in a specified directory and all subderictories. Root directory of drive might be chosen and in this case I want to skip system folders like "Windows" and "AppData" (some other?).

This code looks reasonable to me. But I want to make sure that it is done right.

added 92 characters in body
Source Link
public static List<string> FindSupportedFiles(string root)
{
    // class from library 
    var optimizer = new ImageOptimizer();

    Queue<string> folders = new Queue<string>();
    List<string> files = new List<string>();
 
    folders.Enqueue(root);
    while (folders.Count != 0)
    {
        string currentFolder = folders.Dequeue();
 
        if (currentFolder.Contains(":\\Windows") ||
             (currentFolder.Contains(":\\Users") && currentFolder.Contains("\\AppData")))
        {
            if (folders.Count != 0) currentFolder = folders.Dequeue();
            continue;
        }
 
        string[] filesInCurrent = null;
        try
        {
            filesInCurrent = Directory.GetFiles(currentFolder, "*.*");
        }
        // Ignore this exceptions
        catch (UnauthorizedAccessException) { }
        catch (PathTooLongException) { }
        catch (DirectoryNotFoundException) { }

        foreach (string f in filesInCurrent)
            if (optimizer.IsSupported(f)) files.Add(f);

        string[] foldersInCurrent = null;
        try
        {
            foldersInCurrent = Directory.GetDirectories(currentFolder);
        }
        // Ignore this exceptions
        catch (UnauthorizedAccessException) { }
        catch (PathTooLongException) { }
        catch (DirectoryNotFoundException) { }

        foreach (string _current in foldersInCurrent)
            folders.Enqueue(_current);
    }
    return files;
}
public static List<string> FindSupportedFiles(string root)
{
    Queue<string> folders = new Queue<string>();
    List<string> files = new List<string>();
 
    folders.Enqueue(root);
    while (folders.Count != 0)
    {
        string currentFolder = folders.Dequeue();
 
        if (currentFolder.Contains(":\\Windows") ||
             (currentFolder.Contains(":\\Users") && currentFolder.Contains("\\AppData")))
        {
            if (folders.Count != 0) currentFolder = folders.Dequeue();
            continue;
        }
 
        string[] filesInCurrent = null;
        try
        {
            filesInCurrent = Directory.GetFiles(currentFolder, "*.*");
        }
        // Ignore this exceptions
        catch (UnauthorizedAccessException) { }
        catch (PathTooLongException) { }
        catch (DirectoryNotFoundException) { }

        foreach (string f in filesInCurrent)
            if (optimizer.IsSupported(f)) files.Add(f);

        string[] foldersInCurrent = null;
        try
        {
            foldersInCurrent = Directory.GetDirectories(currentFolder);
        }
        // Ignore this exceptions
        catch (UnauthorizedAccessException) { }
        catch (PathTooLongException) { }
        catch (DirectoryNotFoundException) { }

        foreach (string _current in foldersInCurrent)
            folders.Enqueue(_current);
    }
    return files;
}
public static List<string> FindSupportedFiles(string root)
{
    // class from library 
    var optimizer = new ImageOptimizer();

    Queue<string> folders = new Queue<string>();
    List<string> files = new List<string>();
 
    folders.Enqueue(root);
    while (folders.Count != 0)
    {
        string currentFolder = folders.Dequeue();
 
        if (currentFolder.Contains(":\\Windows") ||
             (currentFolder.Contains(":\\Users") && currentFolder.Contains("\\AppData")))
        {
            if (folders.Count != 0) currentFolder = folders.Dequeue();
            continue;
        }
 
        string[] filesInCurrent = null;
        try
        {
            filesInCurrent = Directory.GetFiles(currentFolder, "*.*");
        }
        // Ignore this exceptions
        catch (UnauthorizedAccessException) { }
        catch (PathTooLongException) { }
        catch (DirectoryNotFoundException) { }

        foreach (string f in filesInCurrent)
            if (optimizer.IsSupported(f)) files.Add(f);

        string[] foldersInCurrent = null;
        try
        {
            foldersInCurrent = Directory.GetDirectories(currentFolder);
        }
        // Ignore this exceptions
        catch (UnauthorizedAccessException) { }
        catch (PathTooLongException) { }
        catch (DirectoryNotFoundException) { }

        foreach (string _current in foldersInCurrent)
            folders.Enqueue(_current);
    }
    return files;
}
deleted 19 characters in body
Source Link

I need to get list of all files supported by some libary (images) in specified directory and all subderictories. Root directory of drive might be chosen and in this case I want to skip system folders like "Windows" and "AppData" (some other?).

For such task I can use Directory.GetFiles() but problem is - it will stop after getting any exception. In this case I need to just ignore noncritical exceptions and skip folders that cause this exception.

This code looks resonable to me. But I want to make sure that it done right.

Please tell me if it should be changed somehow.

public static List<string> FindSupportedFiles(string root)
{
    Queue<string> folders = new Queue<string>();
    List<string> files = new List<string>();
 
    folders.Enqueue(root);
    while (folders.Count != 0)
    {
        string currentFolder = folders.Dequeue();
 
        if (currentFolder.Contains(":\\Windows") ||
             (currentFolder.Contains(":\\Users") &&
              currentFolder.Contains("\\AppData")))
        {
            if (folders.Count != 0) currentFolder = folders.Dequeue();
            continue;
        }
 
        string[] filesInCurrent = null;
        try
        {
            filesInCurrent = Directory.GetFiles(currentFolder, "*.*");
        }
        // Ignore this exceptions
        catch (UnauthorizedAccessException) { }
        catch (PathTooLongException) { }
        catch (DirectoryNotFoundException) { }

        foreach (string f in filesInCurrent)
            if (optimizer.IsSupported(f)) files.Add(f);

        string[] foldersInCurrent = null;
        try
        {
            foldersInCurrent = Directory.GetDirectories(currentFolder);
        }
        // Ignore this exceptions
        catch (UnauthorizedAccessException) { }
        catch (PathTooLongException) { }
        catch (DirectoryNotFoundException) { }

        foreach (string _current in foldersInCurrent)
            folders.Enqueue(_current);
    }
    return files;
}

I need to get list of all files supported by some libary (images) in specified directory and all subderictories. Root directory of drive might be chosen and in this case I want to skip system folders like "Windows" and "AppData" (some other?).

For such task I can use Directory.GetFiles() but problem is - it will stop after getting any exception. In this case I need to just ignore noncritical exceptions and skip folders that cause this exception.

This code looks resonable to me. But I want to make sure that it done right.

Please tell me if it should be changed somehow.

public static List<string> FindSupportedFiles(string root)
{
    Queue<string> folders = new Queue<string>();
    List<string> files = new List<string>();
 
    folders.Enqueue(root);
    while (folders.Count != 0)
    {
        string currentFolder = folders.Dequeue();
 
        if (currentFolder.Contains(":\\Windows") ||
             (currentFolder.Contains(":\\Users") &&
              currentFolder.Contains("\\AppData")))
        {
            if (folders.Count != 0) currentFolder = folders.Dequeue();
            continue;
        }
 
        string[] filesInCurrent = null;
        try
        {
            filesInCurrent = Directory.GetFiles(currentFolder, "*.*");
        }
        // Ignore this exceptions
        catch (UnauthorizedAccessException) { }
        catch (PathTooLongException) { }
        catch (DirectoryNotFoundException) { }

        foreach (string f in filesInCurrent)
            if (optimizer.IsSupported(f)) files.Add(f);

        string[] foldersInCurrent = null;
        try
        {
            foldersInCurrent = Directory.GetDirectories(currentFolder);
        }
        // Ignore this exceptions
        catch (UnauthorizedAccessException) { }
        catch (PathTooLongException) { }
        catch (DirectoryNotFoundException) { }

        foreach (string _current in foldersInCurrent)
            folders.Enqueue(_current);
    }
    return files;
}

I need to get list of all files supported by some libary (images) in specified directory and all subderictories. Root directory of drive might be chosen and in this case I want to skip system folders like "Windows" and "AppData" (some other?).

For such task I can use Directory.GetFiles() but problem is - it will stop after getting any exception. In this case I need to just ignore noncritical exceptions and skip folders that cause this exception.

This code looks resonable to me. But I want to make sure that it done right.

Please tell me if it should be changed somehow.

public static List<string> FindSupportedFiles(string root)
{
    Queue<string> folders = new Queue<string>();
    List<string> files = new List<string>();
 
    folders.Enqueue(root);
    while (folders.Count != 0)
    {
        string currentFolder = folders.Dequeue();
 
        if (currentFolder.Contains(":\\Windows") ||
             (currentFolder.Contains(":\\Users") && currentFolder.Contains("\\AppData")))
        {
            if (folders.Count != 0) currentFolder = folders.Dequeue();
            continue;
        }
 
        string[] filesInCurrent = null;
        try
        {
            filesInCurrent = Directory.GetFiles(currentFolder, "*.*");
        }
        // Ignore this exceptions
        catch (UnauthorizedAccessException) { }
        catch (PathTooLongException) { }
        catch (DirectoryNotFoundException) { }

        foreach (string f in filesInCurrent)
            if (optimizer.IsSupported(f)) files.Add(f);

        string[] foldersInCurrent = null;
        try
        {
            foldersInCurrent = Directory.GetDirectories(currentFolder);
        }
        // Ignore this exceptions
        catch (UnauthorizedAccessException) { }
        catch (PathTooLongException) { }
        catch (DirectoryNotFoundException) { }

        foreach (string _current in foldersInCurrent)
            folders.Enqueue(_current);
    }
    return files;
}
added 213 characters in body
Source Link
Loading
edited title
Link
Loading
added 16 characters in body
Source Link
Loading
added 54 characters in body
Source Link
Loading
deleted 201 characters in body
Source Link
Loading
Source Link
Loading