how to check if the item [name & subItem's tex

2019-09-02 18:36发布

问题:

i making a file transfer (server-client) application ..
i have two listviewS to explore Local PC and Remote PC .. before send/receive the items..
i need to check if there's another file or folder has the same name at the destination path.. when i press on the button [send or receive] the item added to a list.. then when i press on button [Start Transfer] .. it starts.

so the AddItems Method called when i press the button Receive or Send .. i get the SelectedItems from the source ListView .. and the Items of the destination ListView ... then i check for each item in SelectedItems if it is exists in Items

i tried to use

items.Contain(item)

but it didn't work it always gave me false even if the item is already exists.
so i used items.ContainKey and it worked .. but in case that i have a file named "Temp" with no extension and a folder in destination path also named "Temp" .. it will returns True .. and that's 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 set to true when the user clicked on [Yes to all] button in the confirm dialogbox.
TransferType is just to mark the item if it's going to use SendMethod or ReceiveMethod

public enum TransferType
    {
        Send , Receive
    };

so how do i fix that .. should i use a custom method instead of [Contains] that checks for the name and for the type (file or folder) because each item is already has a subItem which tell if it is a folder or a file

thanks in advance.

回答1:

Please try this

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);
    }


回答2:

One quick Idea.

You could utilize your Tag-Property to contain more than just the Transfer-Type.

Since it can contain Objects, you could Create a custom class containing your transfer-type and also more information about the entry. IsDirectory for example and you could utilize that at a later point.

Hope that helps Sascha