如何使用c#列出列表框中的子目录 [英] How to list sub directories in listbox using c#

查看:59
本文介绍了如何使用c#列出列表框中的子目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





请建议如何使用C#列出列表框子目录中的子目录和文件。

Hi,

Please advice how to list the sub-directories and files present inside the sub-directories in listbox using C#.

推荐答案

请参阅我对该问题的评论。阅读本文怎么样: http:// msdn .microsoft.com / zh-cn / library / system.io.directory.getdirectories%28v = vs.110%29.aspx [ ^ ]?



-SA
Please see my comment to the question. How about reading this: http://msdn.microsoft.com/en-us/library/system.io.directory.getdirectories%28v=vs.110%29.aspx[^]?

—SA


在这里。尝试并告诉我你是否需要其他任何东西:



here you are. try it and let me know if you need anything else:

private void Form1_Load(object sender, EventArgs e)
        {
            var sb = new StringBuilder();
            DirSearch(@"C:\Users\vitor\Documents\Visual Studio 2012\Projects\WindowsFormsApplication2", sb);

            //save on a txt file
            File.WriteAllText(@"C:\Users\vitor\Desktop\output.txt", sb.ToString());
        }

        static string ultimaLinha = "";
        static void DirSearch(string sDir, StringBuilder sb)
        {
            try
            {
                foreach (string d in Directory.GetDirectories(sDir))
                {
                    string u = d.Substring(0, d.LastIndexOf("\\"));
                    if (u != ultimaLinha)
                    {
                        //files
                        foreach (string file in Directory.GetFiles(d))
                            sb.AppendLine(file);
                        //directory only
                        //sb.AppendLine(d);
                    }
                    ultimaLinha = u;
                    DirSearch(d, sb);
                }
            }
            catch (System.Exception excpt)
            {
                Console.WriteLine(excpt.Message);
            }
        }


这篇关于如何使用c#列出列表框中的子目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆