“Linq to Directories/Files” – Enumerating Directories and Files with Delphi Prism and .NET 4

Let's share the knowledge with your friends
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

.NET 4 allow you to enumerate directories and files by using methods that return an enumerable collection of strings of their names, also you can use methods that return an enumerable collection of DirectoryInfo, FileInfo, or FileSystemInfo objects, these objects are very powerfull and make easy the development. Before .NET 4 you could only obtain arrays of these collections. Enumerable collections provide better performance than arrays.

Delphi Prism XE provide full support for .NET 4. the follow examples shows how to enumerate Directories and Files.

Enumareting directory names – this samples list all directories under “c:\Program Files\”

class method ConsoleApp.Main(args: array of System.String);
begin
  try
    var dirPath: System.String := 'c:\Program Files';

    // LINQ query.
    var dirs :=  from dir in Directory.EnumerateDirectories(dirPath) select dir;

    // Show results.
    for each dir in dirs do begin
      // Remove path information from string.
      Console.WriteLine('{0}', dir.Substring(dir.LastIndexOf('\') + 1))
    end;
    Console.WriteLine('{0} directories found.', dirs.Count().ToString());

    // Optionally create a List collection.
    var workDirs: List := new List(dirs)
  except
    on UAEx: UnauthorizedAccessException do begin
      Console.WriteLine(UAEx.Message)
    end;
    on PathEx: PathTooLongException do begin
      Console.WriteLine(PathEx.Message)
    end;
  end;
  Console.ReadKey;

end;

Enumerating file names in all directories – this sample search all TXT files which contains the word “license”

class method ConsoleApp.Main(args: array of System.String);
begin

  try

    var files :=  from sfile in Directory.EnumerateFiles('c:\', '*.txt', SearchOption.AllDirectories)
                  from line in File.ReadLines(sfile)
                  where line.Contains('license')
                  select new class( File := sfile, Line := line);

    for each f in files do begin
      Console.WriteLine('{0}'#9'{1}', f.File, f.Line)
    end;

    Console.WriteLine('{0} files found.', files.Count().ToString());
  except
    on UAEx: UnauthorizedAccessException do begin
      Console.WriteLine(UAEx.Message);
    end;
    on PathEx: PathTooLongException do begin
      Console.WriteLine(PathEx.Message);
    end;
  end;

  Console.ReadKey;

end;

Enumerating a collection of DirectoryInfo objects – this sample list all directories created before April of 2010

class method ConsoleApp.Main(args: array of string);
begin

  var dirPrograms: DirectoryInfo := new DirectoryInfo('c:\program files');
  var StartOf2010: DateTime := new DateTime(2010, 4, 1);

  // LINQ query for all directories created before April of 2010.
  var dirs :=  from dir in dirPrograms.EnumerateDirectories()
               where dir.CreationTimeUtc < StartOf2010
               select new class (ProgDir := dir);
  // Show results.
  for each di in dirs do begin
    Console.WriteLine('{0}', di.ProgDir.Name)
  end;

  Console.ReadKey;
end;

You can download the Delphi Prism 2011 Trial here.


Let's share the knowledge with your friends
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
4 replies
  1. Jens Mühlenhoff
    Jens Mühlenhoff says:

    For portability you should always use ‘Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles))’ instead of ‘C:\Program Files\’.

    Reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.