-1

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.

Dropdown list with duplicates

halfer
  • 19,824
  • 17
  • 99
  • 186
joey.coyle
  • 107
  • 1
  • 9
  • 1
    You should be able to use the [Path.GetFileNameWitoutExtension](https://msdn.microsoft.com/en-us/library/system.io.path.getfilenamewithoutextension(v=vs.110).aspx) to get just the file names without the paths. – DHP Dec 11 '15 at 17:05
  • I just realized I posted the wrong black of code. The code I posted is for a gridlist. Here is the code for the dropdown list: – joey.coyle Dec 11 '15 at 17:33
  • I used balexandre code below and how my list does not show file extension. However, it still shows duplicate names. Any suggestions? – joey.coyle Dec 11 '15 at 19:13
  • Can you post your updated code? The code @balexandre posted should be working for you. – DHP Dec 11 '15 at 20:18
  • The code above in my orginal post has been updated – joey.coyle Dec 11 '15 at 21:03
  • I think your problem is in this line `var item = new ListItem(Path.GetFileNameWithoutExtension(filePath), filePath)`. Since you are including the original file path ('video-1.mp4') along with the file name without the extension, this item will be unique. In @balexandre's code, the original file path isn't getting added to the ListItem. Try removing the original file path from the ListItem and see if that fixes your problem. – DHP Dec 11 '15 at 21:12

2 Answers2

1

Some pseudocode (fleshing it out/making it real is an exercise left to the poster (you)):

protected void BindGrid()
{
  string[] filePaths = Directory.GetFiles(Server.MapPath("~/Uploads/"));
  List<ListItem> files = new List<ListItem>();
  foreach (string filePath in filePaths)
  { 
    int dotPos = filePath.IndexOf('.');
    String sansExt = filePath.Substring(1, dotPos);
    if (!files.Contains[sansExt]
    {
        files.Add(new ListItem(Path.GetFileName(filePath), filePath));
    }
  }
  GridView1.DataSource = files;
  GridView1.DataBind();
}

IOW, find where the "dot" is; strip the extension off the filename by copying from the beginning to that location; see if it's already in the generic list of string; if it's not, add it. Make sure that the "." is included in the string you search the generic list for, so that only the fullname matches (not a portion thereof).

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • Thank your you suggestion but I need a complete solution. I am not in school and this is not an assignment. work as a customer service representative and picked this up as a side project to display training videos for the customers I work with. – joey.coyle Dec 11 '15 at 17:30
  • @joey.coyle: the response you've given to both answers here shows that you have the wrong view of what Stack Overflow is for. It is not a source of free labour, and volunteers are not sitting around the world waiting for your demands. All question askers here are expected to make an initial effort, accept the assistance that is provided to them gratefully, and to make a subsequent effort to incorporate what they have learned into their own work. – halfer Mar 30 '17 at 20:51
1

you need to sort them, either when building the list or on the output...

in the Output:

GridView1.DataSource = files.Select(x => x.Text.Split('.')[0]).Distinct();

using LinqPad:

enter image description here

in the List building, you can even use GetFileNameWithoutExtension

foreach (string filePath in filePaths)
{
    var name = filePath.Split('.')[0];
    var item = new ListItem(name);
    if (!files.Contains(item))
        files.Add(item);
}

using LinqPad:

enter image description here

balexandre
  • 73,608
  • 45
  • 233
  • 342
  • Balexandre, OK, after updating my code with your suggestion, the list no longer shows file extension but does show duplicate file names. I will be able to mark your answer as the correct one if you can add code to remove duplicate names from the dropdown as this was part of my original question. – joey.coyle Dec 11 '15 at 19:09
  • @joey.coyle I have no idea what do you want. Just added 2 images so you can see the input and output of what I said... – balexandre Dec 11 '15 at 19:34
  • I just added a link to a screenshot of my dropdown showing duplicate names. – joey.coyle Dec 11 '15 at 21:27