そうですね、ListViewのItemsプロパティを繰り返し処理してから、各アイテムのSubitemsプロパティを繰り返し処理し、最後にサブアイテムのTextプロパティを確認することができます。
もう1つのオプションは、すでに追加されているアイテムをリストに保存し、追加するアイテムがすでにリストに含まれているかどうかを確認することです。
編集:要求に応じて、以下にサンプルコードを追加しました。
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...
}
私はコードをテストしなかったので、タイプミスがある可能性があります。その場合は、コメントを追加して修正します(ただし、最初に自分で理解しようとした方がよいでしょう:))。