VB.NET:列出ListBox中的所有项目文件夹

时间:2016-06-10 20:25:16

标签: vb.net visual-studio-2013

我想知道如何列出root

ListBox文件夹中的每个文件夹

我一直在谷歌周围搜索,但我什么都不懂,所以它根本没有任何帮助。我目前的代码是

Private Sub ListBox1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ' make a reference to a directory
    Dim di As New IO.DirectoryInfo("C:\\TTC\projects\")
    Dim diar1 As IO.FileInfo() = di.GetFiles()
    Dim dra As IO.FileInfo

    'list the names of all files in the specified directory
    For Each dra In diar1
        ListBox1.Items.Add(dra)
    Next
End Sub

我使用的导入是System.IOSystem.Collections

似乎还没有任何效果,任何想法?

修改

我制作的脚本是查找文件而不是文件夹,所以只需将所有内容更改为目录而不是文件

2 个答案:

答案 0 :(得分:0)

如果要列出所有文件夹和子文件夹的路径:

#include <iostream>
#include <map>

typedef int (*funcp)(int id, int p1, int p2);
// using typedef instead of using on purpose because that's what'll be in
// the Windows API

class test
{
private:
    static std::map<int, test*> tests; // our map of id -> test objects
    int id; // this test's id. Only included for proof
public:
    test(int id):id(id)
    {
        tests[id] = this; // put in map. In the real world test for collision first
    }

    // function that will eventually be called
    int func(int p1, int p2)
    {
        (void) p1;
        (void) p2;
        std::cout << "Called func for " << id << std::endl;
        return 0;
    }

    // static method that can be called by API
    static int staticfunc(int id, int p1, int p2)
    {
        // look up object and call non-static method
        // I recommend at() instead of [] because you get a catchable exception
        // rather than a crash over null pointer if id is unknown.
        return tests.at(id)->func(p1, p2);
    }
};

// allocating storage for static map
std::map<int, test*> test::tests;

// function mocking up API. Takes a function and an identifier used by called function
int caller(funcp func,
           int id)
{
    return func(id, 100, 20); // just calls the function
}

// tester
int main(void)
{
    test a(1); // create a test
    test b(2); // create a test

    try
    {
        caller(test::staticfunc, 2); // call test 2 through API
        caller(test::staticfunc, 1); // call test 1 through API
        caller(test::staticfunc, 10); // throw exception over unknown id
    }
    catch (std::exception &e)
    {
        std::cout << "Failed call: " << e.what() << std::endl;
    }
}

如果您只想列出文件夹名称,请尝试以下操作:

Called func for 2
Called func for 1
Failed call: map::at

答案 1 :(得分:0)

我认为你需要一个递归例程。尝试这样的事情:

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ListBox1.Items.Clear()
        ListBox1.BeginUpdate()
        FillListBox("C:\TTC\projects\")
        ListBox1.EndUpdate()
    End Sub

    Private Sub FillListBox(folder As String)
        Dim fi As New IO.DirectoryInfo(folder)
        ListBox1.Items.Add(fi.FullName)

        For Each f In fi.GetDirectories
            FillListBox(f.FullName)
        Next
    End Sub

End Class