SORU
19 Mayıs 2011, PERŞEMBE


Hızlı belirli yolundaki tüm dosyaları ve dizinleri bul

C yedek bir uygulama yaratıyorum# tarar bir dizin. Bir dizindeki tüm dosyaları ve alt almak için böyle bir şey için kullanmadan önce

DirectoryInfo di = new DirectoryInfo("A:\\");
var directories= di.GetFiles("*", SearchOption.AllDirectories);

foreach (FileInfo d in directories)
{
       //Add files to a list so that later they can be compared to see if each file
       // needs to be copid or not
}

Tek sorun bazen bir dosya erişilemedi ve çeşitli hatalar alıyorum. aldığım hata ile ilgili bir örnek:error

Sonuç olarak, geçerli dizindeki tüm dosyaları tarar bu özyinelemeli bir yöntem oluşturdum. Bu dizinde dizinleri nerede olursa o yöntemi tekrar bu dizin aktarımı denir. Güzel bir şey bu yöntem olduğunu edebilirim yerini dosyaları bir try catch bloğu bana Seçenek Ekle o dosyaları için bir Liste varsa hiçbir hataları ve ekleme dizin için başka bir liste olsaydı hataları.

try
{
    files = di.GetFiles(searchPattern, SearchOption.TopDirectoryOnly);               
}
catch
{
     //info of this folder was not able to get
     lstFilesErrors.Add(sDir(di));
     return;
}

Bu yöntem iyi çalışıyor yani tek sorun ben büyük bir dizin tarama zaman çok zaman alır. Nasıl bu süreci hızlandırabilir miyim? Benim gerçek yöntemi olarak bu.

private void startScan(DirectoryInfo di)
{
    //lstFilesErrors is a list of MyFile objects
    // I created that class because I wanted to store more specific information
    // about a file such as its comparePath name and other properties that I need 
    // in order to compare it with another list

    // lstFiles is a list of MyFile objects that store all the files
    // that are contained in path that I want to scan

    FileInfo[] files = null;
    DirectoryInfo[] directories = null;
    string searchPattern = "*.*";

    try
    {
        files = di.GetFiles(searchPattern, SearchOption.TopDirectoryOnly);               
    }
    catch
    {
        //info of this folder was not able to get
        lstFilesErrors.Add(sDir(di));
        return;
    }

    // if there are files in the directory then add those files to the list
    if (files != null)
    {
        foreach (FileInfo f in files)
        {
            lstFiles.Add(sFile(f));
        }
    }


    try
    {
        directories = di.GetDirectories(searchPattern, SearchOption.TopDirectoryOnly);
    }
    catch
    {
        lstFilesErrors.Add(sDir(di));
        return;
    }

    // if that directory has more directories then add them to the list then 
    // execute this function
    if (directories != null)
        foreach (DirectoryInfo d in directories)
        {
            FileInfo[] subFiles = null;
            DirectoryInfo[] subDir = null;

            bool isThereAnError = false;

            try
            {
                subFiles = d.GetFiles();
                subDir = d.GetDirectories();

            }
            catch
            {
                isThereAnError = true;                                                
            }

            if (isThereAnError)
                lstFilesErrors.Add(sDir(d));
            else
            {
                lstFiles.Add(sDir(d));
                startScan(d);
            }


        }

}

Eğer bir şey gibi özel durumu idare etmeye çalışırsam sorun karınca:

DirectoryInfo di = new DirectoryInfo("A:\\");
FileInfo[] directories = null;
            try
            {
                directories = di.GetFiles("*", SearchOption.AllDirectories);

            }
            catch (UnauthorizedAccessException e)
            {
                Console.WriteLine("There was an error with UnauthorizedAccessException");
            }
            catch
            {
                Console.WriteLine("There was antother error");
            }

Eğer bir istisna o zaman hiçbir dosya almak oluşursa.

CEVAP
19 Mayıs 2011, PERŞEMBE


Bu yöntem çok daha hızlı. Sadece bir dizinde çok sayıda dosya yerleştirirken tel. :\ Benim harici sabit disk neredeyse 1 terabit içeren çok sayıda dosya ile uğraşırken, büyük bir fark yaratıyor.

static void Main(string[] args)
{
    DirectoryInfo di = new DirectoryInfo("A:\\");
    FullDirList(di, "*");
    Console.WriteLine("Done");
    Console.Read();
}

static List<FileInfo> files = new List<FileInfo>();  // List that will hold the files and subfiles in path
static List<DirectoryInfo> folders = new List<DirectoryInfo>(); // List that hold direcotries that cannot be accessed
static void FullDirList(DirectoryInfo dir, string searchPattern)
{
    // Console.WriteLine("Directory {0}", dir.FullName);
    // list the files
    try
    {
        foreach (FileInfo f in dir.GetFiles(searchPattern))
        {
            //Console.WriteLine("File {0}", f.FullName);
            files.Add(f);                    
        }
    }
    catch
    {
        Console.WriteLine("Directory {0}  \n could not be accessed!!!!", dir.FullName);                
        return;  // We alredy got an error trying to access dir so dont try to access it again
    }

    // process each directory
    // If I have been able to see the files in the directory I should also be able 
    // to look at its directories so I dont think I should place this in a try catch block
    foreach (DirectoryInfo d in dir.GetDirectories())
    {
        folders.Add(d);
        FullDirList(d, searchPattern);                    
    }

}

Bu arada yorumlarınız Jim Mischel bu teşekkür aldım

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • jbignacio

    jbignacio

    13 Mart 2006
  • spyib

    spyib

    9 Ocak 2007
  • Top10Series

    Top10Series

    26 Kasım 2008