Search code examples
c#winformslistviewopenfiledialog

OpenFileDialog - selecting same file twice


I am developing a windows forms application which uses OpenFileDialog to select files and drop it to a ListView.

The user should not be able to add same file to the listview twice. This should NOT happen:

This should NOT happen!!!!

How can this be done??


Solution

  • Try out this code.

    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        var file = openFileDialog1.FileName;
        if (listView1.FindItemWithText(file) == null)
            listView1.Items.Add(file);
    }
    

    You can also add else with a message box informing user about choosing a duplicate file.