Get Files and Folders From a Directory

samueal -1 Tallied Votes 488 Views Share

This is the code snippet used to get the files/folder present in a particular folder/sub directories.

using System.IO;
using System;

namespace folder_information
{
    public partial class folder
    {
//Function to get all the folders present in that particular location,Use
string[] folders = Directory.GetDirectories(@"C:\myfolder\Projects");

//Function to get all the files present in that particular location,Use
string[] files = Directory.GetFiles(@"C:\myfolder\Projects");

//Function to get all the files present in that particular location with particular extension,Use
string[] cs_files= Directory.GetFiles(@"C:\myfolder\Projects","*.cs");

//Function to get all the files present in that particular location and its subfolders with particular extension,Use
string[] cs_files= Directory.GetFiles(@"C:\myfolder\Projects","*.cs",SearchOption.AllDirectories);[/CODE]
}
}
CsharpChico 1 Junior Poster in Training
using System.IO;
    using System;

namespace folder_information
{
    public partial class folder
    {
        protected DirectoryInfo _Directory;

        public folder(string directory) { _Directory = new DirectoryInfo(directory); }

        //Function to get all the folders present in that particular location,Use       
        public DirectoryInfo[] Directories { get { return _Directory.GetDirectories(); } }

        //Function to get all the files present in that particular location,Use        
        public FileInfo[] Files() { return _Directory.GetFiles(); } 

        //Function to get all the files present in that particular location with particular extension,Use
        public FileInfo[] Files(string searchPattern) { return _Directory.GetFiles(searchPattern); }        

        //Function to get all the files present in that particular location and its subfolders with particular extension,Use
        public FileInfo[] Files(string searchPatern, SearchOption searchOption) { return _Directory.GetFiles(searchPatern, searchOption); }
        
    }
}
CsharpChico 1 Junior Poster in Training
using System.IO;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace folder_information
{
    public partial class folder
    {
        protected DirectoryInfo _Directory;

        public folder(string directory) { _Directory = new DirectoryInfo(directory); }
                
        public DirectoryInfo[] Directories { get { return _Directory.GetDirectories(); } }
                
        public FileInfo[] Files() { return _Directory.GetFiles(); } 
                
        public FileInfo[] Files(string searchPattern) { return _Directory.GetFiles(searchPattern); }        
                
        public FileInfo[] Files(string searchPatern, SearchOption searchOption) { return _Directory.GetFiles(searchPatern, searchOption); }

        public string[] DirectoryCollection
        {
            get
            {
                List<String> directory = new List<string>();
                foreach (DirectoryInfo info in Directories)
                {
                    directory.Add(info.FullName);
                }
                return directory.ToArray();
            }
        }

        public ReadOnlyCollection<String> ReadOnlyDirectoryList
        {
            get
            {
                List<String> directory = new List<string>();
                foreach (DirectoryInfo info in Directories)
                {
                    directory.Add(info.FullName);
                }
                return directory.AsReadOnly();
            }
        }

        public ReadOnlyCollection<String> ReadOnlyFileList()
        {
            List<String> file = new List<string>();
            foreach (FileInfo info in Files())
            {
                file.Add(info.FullName);
            }
            return file.AsReadOnly();
        }

        public string[] FileCollection()
        {
            List<String> file = new List<string>();
            foreach (FileInfo info in Files())
            {
                file.Add(info.FullName);
            }
            return file.ToArray();
        }

        public ReadOnlyCollection<String> ReadOnlyFileList(string searchPattern)
        {
            List<String> file = new List<string>();
            foreach (FileInfo info in Files(searchPattern))
            {
                file.Add(info.FullName);
            }
            return file.AsReadOnly();
        }

        public string[] FileCollection(string searchPattern)
        {
            List<String> file = new List<string>();
            foreach (FileInfo info in Files(searchPattern))
            {
                file.Add(info.FullName);
            }
            return file.ToArray();
        }

        public ReadOnlyCollection<String> ReadOnlyFileList(string searchPattern, SearchOption searchOption)
        {
            List<String> file = new List<string>();
            foreach (FileInfo info in Files(searchPattern, searchOption))
            {
                file.Add(info.FullName);
            }
            return file.AsReadOnly();
        }

        public string[] FileCollection(string searchPattern, SearchOption searchOption)
        {
            List<String> file = new List<string>();
            foreach (FileInfo info in Files(searchPattern, searchOption))
            {
                file.Add(info.FullName);
            }
            return file.ToArray();
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.