列表框不存在C#

时间:2017-08-02 11:16:02

标签: c#

我有这段代码

public static List<string> GetAllFilesFromFolder(string root, bool searchSubfolders)
{
    Queue<string> folders = new Queue<string>();
    List<string> files = new List<string>();
    folders.Enqueue(root);
    while (folders.Count != 0)
    {
        string currentFolder = folders.Dequeue();
        try
        {
            string[] filesInCurrent = System.IO.Directory.GetFiles(currentFolder, "*.*", System.IO.SearchOption.TopDirectoryOnly);
            files.AddRange(filesInCurrent);
        }
        catch
        {
            // Do Nothing
        }
        try
        {
            if (searchSubfolders)
            {
                string[] foldersInCurrent = System.IO.Directory.GetDirectories(currentFolder, "*.*", System.IO.SearchOption.TopDirectoryOnly);
                foreach (string _current in foldersInCurrent)
                {
                    folders.Enqueue(_current);
                }
            }
        }
        catch
        {
            // Do Nothing
        }
    }
    return files;
}

代码列出了List<string>的所有文件,但我需要在listbox.items中看到它,我试过了

foreach(string foo in files)
    listbox1.items.add(foo)

问题是它没有识别listbox1并且一直说它不存在。我该怎么做?它是一个Windows窗体应用程序。

0 个答案:

没有答案