How to read all folder and subfolder path C#

Today i want to show how you can get folder path and all subfolder path. The process is more efficient and faster because im using IEnumerable type to determine if path has sub directory or not.

I write this example in c# using window form. Here is my winform GUI :


The sample Output

----------------------------------
Directory List
Start Path : C:\Android Project
----------------------------------
List file and directory

Directory and Subdirectory Name
C:\Android Project\FirstProject
C:\Android Project\FirstProject\.metadata
C:\Android Project\FirstProject\.metadata\.plugins
C:\Android Project\FirstProject\.metadata\.plugins\org.eclipse.cdt.core
C:\Android Project\FirstProject\.metadata\.plugins\org.eclipse.cdt.make.core
C:\Android Project\FirstProject\.metadata\.plugins\org.eclipse.core.resources
C:\Android Project\FirstProject\.metadata\.plugins\org.eclipse.core.runtime
C:\Android Project\FirstProject\.metadata\.plugins\org.eclipse.debug.core
C:\Android Project\FirstProject\.metadata\.plugins\org.eclipse.debug.ui
C:\Android Project\FirstProject\.metadata\.plugins\org.eclipse.jdt.core
C:\Android Project\FirstProject\.metadata\.plugins\org.eclipse.jdt.ui
C:\Android Project\FirstProject\.metadata\.plugins\org.eclipse.ui.intro
C:\Android Project\FirstProject\.metadata\.plugins\org.eclipse.ui.workbench
C:\Android Project\FirstProject\.metadata\.plugins\org.eclipse.core.resources\.history
C:\Android Project\FirstProject\.metadata\.plugins\org.eclipse.core.resources\.root
C:\Android Project\FirstProject\.metadata\.plugins\org.eclipse.core.resources\.safetable
C:\Android Project\FirstProject\.metadata\.plugins\org.eclipse.core.resources\.root\.indexes
C:\Android Project\FirstProject\.metadata\.plugins\org.eclipse.core.runtime\.settings

when the button GetFolderName  clicked, the process will be like this :

The Source Code




 private void button1_Click(object sender, EventArgs e)
        {
            string fileNameOutput = "listofDirectory.txt";
            string folderName = Path.GetDirectoryName(textBox1.Text);
            string[] directoryList = Directory.GetDirectories(textBox1.Text);
            string[] filesName = Directory.GetFiles(textBox1.Text);
            if (File.Exists(fileNameOutput))
            {
                File.Delete(fileNameOutput);
            }

            StreamWriter sw = new StreamWriter(fileNameOutput);
            sw.WriteLine("----------------------------------");
            sw.WriteLine("Directory List");
            sw.WriteLine("Start Path : " + textBox1.Text);
            sw.WriteLine("----------------------------------");
            sw.WriteLine("List file and directory");
            if (filesName.Length > 0)
            {
                sw.WriteLine("Files Name");
            }

            writeToFile(sw, filesName);

            if (directoryList.Length > 0)
            {
                sw.WriteLine("\r\nDirectory and Subdirectory Name");
            }
          
            writeToFile(sw, directoryList);
            string[] list = directoryList;
            string[] _temp = null;
            int index = 0;
            for (int level = 0; level < directoryList.Length; level++)
            {
                if (HasSubfoldersAlternate(directoryList[level].ToString()))
                {
                    writeSubfolder(directoryList[level].ToString(), sw);
                }              
            }

            sw.Close();

            DialogResult dr = MessageBox.Show("Open the saved file in an external viewer anyway?", "Open Rendered File", MessageBoxButtons.YesNo);

            if (dr == DialogResult.Yes)
            {
                try
                {
                    System.Diagnostics.Process.Start(fileNameOutput);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);                   
                }
            }
        }
        public void writeSubfolder(string path,StreamWriter sw)
        {
            string[] list = Directory.GetDirectories(path);
            writeToFile(sw, list);
            for (int i = 0; i < list.Length; i++)
            {
                if(HasSubfoldersAlternate(list[i].ToString()))
                {
                    writeSubfolder(list[i].ToString(),sw);
                }
            }
        }
        public void writeToFile(StreamWriter sw, string[] listtoWrite)
        {
            for (int i = 0; i < listtoWrite.Length; i++)
            {
                sw.WriteLine(listtoWrite[i].ToString());
            }
        }
        bool HasSubfoldersAlternate(string path)
        {
            IEnumerable<string> subfolders = Directory.EnumerateDirectories(path);
            return subfolders != null;
        }


Popular posts from this blog

Example to disable save as certain file type in SSRS Report Viewer

How to create DataGrid or GridView in JSP - Servlet

Control Webpart Visible/Enable using macro in Kentico