How to recursively list all the files in a directory in C#?

前端 未结 22 2969
长发绾君心
长发绾君心 2020-11-22 00:07

How to recursively list all the files in a directory and child directories in C#?

22条回答
  •  离开以前
    2020-11-22 00:46

    Listing files and folders to model, custom implementation.
    This creates a full listing of all files and folders starting from your start directory.

    public class DirOrFileModel
        {
            #region Private Members
    
            private string _name;
            private string _location;
            private EntryType _entryType;
    
            #endregion
    
            #region Bindings
    
            public string Name
            {
                get { return _name; }
                set
                {
                    if (value == _name) return;
                    _name = value;
                }
            }
    
            public string Location
            {
                get { return _location; }
                set
                {
                    if (value == _location) return;
                    _location = value;
                }
            }
    
            public EntryType EntryType
            {
                get { return _entryType; }
                set
                {
                    if (value == _entryType) return;
                    _entryType = value;
                }
            }
    
            public ObservableCollection Entries { get; set; }
    
            #endregion
    
            #region Constructor
    
            public DirOrFileModel()
            {
                Entries = new ObservableCollection();
            }
    
            #endregion
        }
    
        public enum EntryType
        {
            Directory = 0,
            File = 1
        }
    

    Method:

     static DirOrFileModel DirSearch(DirOrFileModel startDir)
            {
                var currentDir = startDir;
                try
                {
                    foreach (string d in Directory.GetDirectories(currentDir.Location))
                    {
                        var newDir = new DirOrFileModel
                        {
                            EntryType = EntryType.Directory,
                            Location = d,
                            Name = Path.GetFileName(d)
                        };
                        currentDir.Entries.Add(newDir);
    
                        DirSearch(newDir);
                    }
    
                    foreach (string f in Directory.GetFiles(currentDir.Location))
                    {
                        var newFile = new DirOrFileModel
                        {
                            EntryType = EntryType.File,
                            Location = f,
                            Name = Path.GetFileNameWithoutExtension(f)
                        };
                        currentDir.Entries.Add(newFile);
                    }
    
                }
                catch (Exception excpt)
                {
                    Console.WriteLine(excpt.Message);
                }
                return startDir;
            }
    

    Usage:

    var dir = new DirOrFileModel
                {
                    Name = "C",
                    Location = @"C:\",
                    EntryType = EntryType.Directory
                };
    
                dir = DirSearch(dir);
    

提交回复
热议问题