How to check that the [name & subItem text] element already exists in another View list?

I am making a file transfer application (server-client).
I have two lists for exploring the local PC and the remote PC. before sending / receiving items.
I need to check if another file or folder has the same name on the destination path. when I click on the [send or receive] button, the item is added to the list .. then when I click on the [Start transfer] button .. it starts.

so the AddItems method is called when I click the Receive or Send button .. I get SelectedItems from the source ListView .. and the destination ListView Elements ... then I check each element in SelectedItems if it exists in Elements

enter image description here

I tried to use

items.Contain(item) 

but it didn’t work, it always gave me a lie, even if the element already exists.
so I used items.ContainKey and it worked. But in case I have a file named "Temp" without an extension and a folder in the destination path, also called "Temp" .. it will return True .. and this is my problem ..

 bool YesToAll = false; public void AddItems(ListView.SelectedListViewItemCollection selectedItems, ListView.ListViewItemCollection items,TransferType type,string destPath) { foreach(ListViewItem item in selectedItems) { if (items.ContainsKey(item.Name) && !YesToAll) { MyMessageBox msgbox = new MyMessageBox("Item is already exists .. Do you want to replace (" + item.Text + ") ?"); msgbox.ShowDialog(); if (msgbox.DialogResult == DialogResult.Yes) { Add(item, type, destPath); } else if (msgbox.DialogResult == DialogResult.OK) { YesToAll = true; Add(item, type, destPath); } else if (msgbox.DialogResult == DialogResult.No) { continue; } else { return; } } else { Add(item, type, destPath); } } YesToAll = false; } private void Add(ListViewItem item,TransferType type,string path) { ListViewItem newItem = (ListViewItem)item.Clone(); newItem.ImageIndex = imageList1.Images.Add(item.ImageList.Images[item.ImageIndex],Color.Transparent); newItem.SubItems.Add(type.ToString()); newItem.SubItems.Add(path); newItem.Tag = type; listView1.Items.Add(newItem); } 

YesToAll is true when the user clicked the [Yes for All] button in the confirmation dialog box.
TransferType - it's just to mark an element if it will use SendMethod or ReceiveMethod

 public enum TransferType { Send , Receive }; 

so how can i fix this. Should I use my own method instead of [Contains], which checks the name and type (file or folder), because each item already has a subItem that indicates whether it is a folder or file

early.

0
source share
2 answers

Please, try

 bool YesToAll = false; public void AddItems(ListView.SelectedListViewItemCollection selectedItems, ListView.ListViewItemCollection items,TransferType type,string destPath) { foreach(ListViewItem item in selectedItems) { if (items.ContainsKey(item.Name) && !YesToAll) { ListViewItem lvtemp=items.Find(item.Name)[0]; if((lvTemp.SubItems[0].Text!= "[Folder]" && item.SubItem[0].Text!="[Folder]" ) or (lvTemp.SubItems[0].Text== item.SubItems[0].Text && lvTemp.SubItems[0].Text="[Folder]") ) { MyMessageBox msgbox = new MyMessageBox("Item is already exists .. Do you want to replace (" + item.Text + ") ?"); msgbox.ShowDialog(); if (msgbox.DialogResult == DialogResult.Yes) { Add(item, type, destPath); } else if (msgbox.DialogResult == DialogResult.OK) { YesToAll = true; Add(item, type, destPath); } else if (msgbox.DialogResult == DialogResult.No) { continue; } else { return; } } } else { Add(item, type, destPath); } } YesToAll = false; } private void Add(ListViewItem item,TransferType type,string path) { ListViewItem newItem = (ListViewItem)item.Clone(); newItem.ImageIndex = imageList1.Images.Add(item.ImageList.Images[item.ImageIndex],Color.Transparent); newItem.SubItems.Add(type.ToString()); newItem.SubItems.Add(path); newItem.Tag = type; listView1.Items.Add(newItem); } 
0
source

One quick idea.

You can use your property tag to contain more than just Transfer-Type.

Since it can contain objects, you can create your own class containing your transfer type, as well as additional information about the record. IsDirectory, and you can use this at a later point.

Hope this helps Sascha

+1
source

Source: https://habr.com/ru/post/1400944/


All Articles