Aycan Candar
Aycan Candar

Reputation: 63

Adding file names in a folder to DropDownList. (asp.net)

I tried add the names of files in a folder to one dropdownlist. With this code, the path of the files appears. I tried something to make only the file names appear, but they looked vertical in dropdownlist.

 var files = Directory.GetFiles("d:\\a\\b\\c\\", "*.jpg");
 ddChoose.DataSource = files;
 ddChoose.DataBind();

Upvotes: 1

Views: 250

Answers (2)

Allen  Kuo
Allen Kuo

Reputation: 76

    protected void Page_Load(object sender, EventArgs e)
    {
        var files = Directory.GetFiles(@"d:\temp\11\", "*.jpg")
            .Select(fullName=>System.IO.Path.GetFileNameWithoutExtension(fullName));

        ddChoose.DataSource = files;
        ddChoose.DataBind();
    }

//  private string purgePath(string fullName)
//  {
//      var index = fullName.LastIndexOf('\\');
//      return fullName.Substring(index + 1);
//  }

Upvotes: 1

Allen  Kuo
Allen Kuo

Reputation: 76

    protected void Page_Load(object sender, EventArgs e)
    {
        var files = Directory.GetFiles(@"d:\temp\", "*.jpg");
        IEnumerable<string> data = files.Select(purgePath);

        ddChoose.DataSource = data;
        ddChoose.DataBind();
    }

    private string purgePath(string fullName)
    {
        var index = fullName.LastIndexOf('\\');
        return fullName.Substring(index + 1);
    }

Upvotes: 0

Related Questions