0

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

enter image description here

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.

Murhaf Sousli
  • 12,622
  • 20
  • 119
  • 185

2 Answers2

1

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

  • yea actually .. i've already set subItem[1] in all items for Size like ["15 M.B"] if its a file and ["Folder"] if it's a folder .. so i can check if the item.subItem[1] == "Folder" .. but im trying to avoid creating custom method for that instead of Contains. – Murhaf Sousli Mar 12 '12 at 08:22
  • I am afraid you might have to think about you approach anyway. If you manage to copy temp file side by side to temp folder, you will probably have two entries by the key "temp" in your dictionary, which is not valid. Anyway - Have you considered to use Linq ? See here for an inspiration http://stackoverflow.com/questions/4505602/easiest-way-to-implement-contains-on-a-icollectiontuplet1-t2 – Sascha Berlin Mar 12 '12 at 08:35
  • so you mean Item.Name should be unique .. thanks for noticing me about this .. i will have a revision about the code. – Murhaf Sousli Mar 12 '12 at 08:45
0

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);
    }
PraveenVenu
  • 8,217
  • 4
  • 30
  • 39