How to get list of all folders in a DropdownList in asp.net

Description:-

Get list of all folders in a Dropdownlist. using Directory class we can bind or get directories from Drive and bind into where we want. Here I have bind E: Drive all folder in dropdownlist control.

Default.aspx:-

<div>
  <asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
</div>

Default.aspx.cs:-

protected void Page_Load(object sender, EventArgs e)
{
  foreach (var folder in Directory.GetDirectories(@"E:\"))
  {
    DropDownList1.Items.Add(folder.ToString().Replace(@"E:\", ""));
  }
}

Related Posts

Previous
Next Post »

Thanks for comments.....