views:

43

answers:

5

If I wanted to have a collection that described the (recursive) contents of a root directory, including directories and files, how would I store them? Or do I need to come up with an object that holds: -Current directory -Parent directory -Files in directory

..and slap them all in one big list and manually work out the relationship at runtime from each entries Parent directory.

A: 

The .NET framework already includes Directory/File and DirectoryInfo/FileInfo. But if you really need to roll your own, I'd suggest having an object with Parent, Name, Directories (children) and Files. That way you can happily navigate up and down the structure.

pdr
+1  A: 

If DirectoryInfo and FileInfo classes are not enought then you should use a Tree structure.

Jorge Córdoba
Thanks for that. I should have explicitly stated that the dir/file tree was just an example of what a generic solution would provide for, and the tree structure seems to be it!
George R
A: 

If you want to create something hierarchical you'll have to create a class that own a list of itself.

class MyDirectory
-MyFile[] Files
-MyDirectory Parent
-MyDirectory[] SubDirectories
remi bourgarel
A: 

You can do something like this (the sytax could be a little of, as I mostly code in VB):

List<String> pathOfElements = new List<String>
IO.FileSystemInfo() elementsList = New IO.DirectoryInfo("C:\").GetFileSystemInfos() 
foreach element as FileSystemInfo in elementsList
pathOfElements.add(element.FullPath)
next

With GetFileSystemInfos you can see both directories and files in a gived directory (additionally you can use GetFileSystemInfos(String) to get the files and subdirectories matching the specified search criteria)

And for storing the information regarding files and directories, if you use the full path, all you need is a list of strings.

Ando
A: 

On a first shot i would try something like this:

public class Entry
{
    public string Name { get; private set; }
    public EntryType Type { get; private set; }

    private FileInfo _FileInfo;
    private DirectoryInfo _DirectoryInfo;

    public Entry(FileInfo fileInfo)
    {
        _FileInfo = fileInfo;
        Type = EntryType.FileInfo;
        Name = fileInfo.Name;
    }

    public Entry(DirectoryInfo directoryInfo)
    {
        _DirectoryInfo = directoryInfo;
        Type = EntryType.DirectoryInfo;
        Name = directoryInfo.Name;
    }

    public IEnumerable<Entry> Childs
    {
        get
        {
            if (Type != EntryType.DirectoryInfo
                || _DirectoryInfo == null)
            {
                throw new ArgumentNullException();
            }

            foreach (var directory in _DirectoryInfo.GetDirectories())
            {
                yield return new Entry(directory);
            }

            foreach (var file in _DirectoryInfo.GetFiles())
            {
                yield return new Entry(file);
            }
        }
    }

    public enum EntryType
    {
        None,
        FileInfo,
        DirectoryInfo
    }
}

Warning

This is untested and actually i don't know if you'll run into problems with the yield return new ....

Oliver