I have an asp.net C# webform project. I have created a dropdown list that is bound to a local upload directory. The directory contains video files. Each video has 3 file extension (mp4, ogv, and webm). I only want one of each file name to appear in the list without an extension. Currently my dropdown list looks like this:
video-1.mp4
video-1.ogv
video-1.webm
video-2.mp4
video-2.ogv
video-2.webm
I want the list to look like this:
video-1
video-2
Here is my code behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
if (!IsPostBack)
{
string[] filePaths = Directory.GetFiles(Server.MapPath("~/Uploads/"));
List<ListItem> files = new List<ListItem>();
foreach (string filePath in filePaths)
{
var item = new ListItem(Path.GetFileNameWithoutExtension(filePath), filePath);
if (!files.Contains(item))
files.Add(new ListItem(Path.GetFileName(filePath), filePath));
}
DropDownList1.DataSource = files;
DropDownList1.DataTextField = "";
DropDownList1.DataValueField = "";
DropDownList1.DataBind();
}
}
protected void BindGrid()
{
string[] filePaths = Directory.GetFiles(Server.MapPath("~/Uploads/"));
List<ListItem> files = new List<ListItem>();
foreach (string filePath in filePaths)
{
files.Add(new ListItem(Path.GetFileName(filePath), filePath));
}
GridView1.DataSource = files;
GridView1.DataBind();
}
This is what my current dropdown list looks like. I want to remove the duplicates.