Итак, вы можете выполнить итерацию через свойство Items ListView , а затем свойство Subitems для каждого элемента и, наконец, проверить свойство Text подэлемента..
Другой вариант - сохранить уже добавленные элементы в списке и проверить, содержит ли он уже тот элемент, который вы хотите добавить.
Редактировать : по запросу, добавил пример кода ниже.
private bool _CheckFileName(string fileName)
{
foreach(ListViewItem item in this.myListView.Items)
{
// this is the code when all your subitems are file names - if an item contains only one subitem which is a filename,
// then you can just against that subitem, which is better in terms of performance
foreach(ListViewItem.ListViewSubItem subItem in item.SubItems)
{
// you might want to ignore the letter case
if(String.Equals(fileName, subItem.Text))
{
return false;
}
}
}
return true;
}
using(var ofd = new OpenFileDialog())
{
// ... setup the dialog ...
if(ofd.ShowDialog() == DialogResult.Cancel)
{
// cancel
return;
}
// note that FileOpenDialog.FileName will give you the absolute path of the file; if you want only the file name, you should use Path.GetFileName()
if(!_CheckFileName(ofd.FileName))
{
// file already added
return;
}
// we're cool...
}
Я не тестировал код, поэтому возможно, что у меня есть некоторые опечатки, если так, пожалуйста, добавьте комментарий, и я исправлю его (хотя, возможно, это будетбыло бы лучше, если бы вы сначала попытались сами разобраться:)).