asp:DropDownList从本地目录中获取数据?

时间:2011-06-07 14:21:21

标签: c# asp.net upload

我的页面上有一个下拉列表&希望列表项是Web服务器上本地目录中的文件夹...即....

T:\形式 T:\手册 T:\软件

到目前为止,这是我的代码......

protected void Page_Load(object sender, EventArgs e)
    {
        DirectoryInfo di = new DirectoryInfo("C:/");
        DirectoryInfo[] dirArray = di.GetDirectories();
        DropDownList1.DataSource = dirArray;
        foreach (DirectoryInfo i in dirArray)
        {
            DropDownList1.DataTextField = i.FullName;
            DropDownList1.DataValueField = i.FullName;
        }
    }

解决

protected void Page_Load(object sender, EventArgs e)
    {
        DirectoryInfo di = new DirectoryInfo("C:/");

        DropDownList1.DataSource = di.GetDirectories();

        DropDownList1.DataBind();

        foreach (DirectoryInfo i in di.GetDirectories())
        {
            DropDownList1.DataTextField = i.FullName;
        }

    }

3 个答案:

答案 0 :(得分:3)

我建议使用这样一段代码

  DirectoryInfo di = new DirectoryInfo(@"e:\");
  ddlFolders.DataSource = di.GetDirectories();
  ddlFolders.DataTextField = "Name";
  ddlFolders.DataValueField = "FullName";
  ddlFolders.DataBind();

HTH

答案 1 :(得分:1)

查看

System.IO.DirectoryInfo

System.IO.FileInfo

类。显然,您只能读取Web服务器的文件系统

答案 2 :(得分:1)

您可以使用

List<string> dirList=new List<string>();

DirectoryInfo[] DI = new DirectoryInfo(@"T:\Forms\").GetDirectories("*.*",SearchOption.AllDirectories ) ;
foreach (DirectoryInfo D1 in DI)
{
    dirList.Add(D1.FullName);
}

对所有三个目录执行此操作,然后将数据绑定到列表