Re: Get all files (.NET)
От: slv Украина  
Дата: 07.02.03 07:53
Оценка:
Здравствуйте, 4mbi3nt, Вы писали:

4>эта функция выдаёт array со всеми фаилами (в том числе фаилы из подкаталогов)


4>[vb]

4> Function GetAllFiles(ByVal sPath As String) As String()

А эта просто выводит все файлы из каталога (в том числе фаилы из подкаталогов)

// For Directory.GetFiles and Directory.GetDirectories
// For File.Exists, Directory.Exists

using System;
using System.IO;
using System.Collections;

// Takes an array of file names or directory names on the command line.  
// Determines what kind of name it is and processes it appropriately
public class RecursiveFileProcessor {
    public static void Main(string[] args) {
      foreach(string path in args) {
            if(File.Exists(path)) {
                // This path is a file
            ProcessFile(path); 
         }               
            else if(Directory.Exists(path)) {
                // This path is a directory
                ProcessDirectory(path);
            }
            else {
                Console.WriteLine("{0} is not a valid file or directory.", path);
            }        
        }        
    }


    // Process all files in the directory passed in, and recurse on any directories 
    // that are found to process the files they contain
    public static void ProcessDirectory(string targetDirectory) {
        // Process the list of files found in the directory
        string [] fileEntries = Directory.GetFiles(targetDirectory);
        foreach(string fileName in fileEntries)
           ProcessFile(fileName);

        // Recurse into subdirectories of this directory
        string [] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
      foreach(string subdirectory in subdirectoryEntries)
          ProcessDirectory(subdirectory);
    }
        
    // Real logic for processing found files would go here.
   public static void ProcessFile(string path) {
        Console.WriteLine("Processed file '{0}'.", path);       
   }
}

Это всё элементарно.
Интересней было бы увидеть класс "ДеревоКаталога" атрибутами которого
были бы эти самые папки и файлы. Что здесь интересного? Сам процесс
проектирования и анализа. К примеру:
класс "ДеревоКаталога" состоит из "Папка" и "Файл".
С "Файл" всё понятно. А вот "Папка" опять состоит из "Папка" и "Файл"
имеем ситуацию "у попа была собака ..."
Что здесь за связь — ассоциация, агрегация или композиция?
Как грамотно спроектировать класс?
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.