【发布时间】:2012-03-12 08:05:58
【问题描述】:
我制作了一个文件传输(服务器-客户端)应用程序 ..
我有两个 listviewS 来探索本地 PC 和远程 PC .. 在发送/接收项目之前..
我需要检查是否还有另一个文件或文件夹在目标路径中具有相同的名称..
当我按下按钮 [发送或接收] 添加到列表中的项目.. 然后当我按下按钮 [开始传输] .. 它开始。
所以当我按下按钮 Receive 或 Send 时调用 AddItems 方法 .. 我从源 ListView 获得 SelectedItems .. 和目标 ListView 的 Items 。 ..然后我检查 SelectedItems 中的每个项目是否存在于 Items
我尝试使用
items.Contain(item)
但它不起作用,即使该项目已经存在,它总是给我错误。
所以我使用了 items.ContainKey 并且它有效.. 但是如果我有一个名为“Temp”的文件,没有扩展名,并且目标路径中的文件夹也名为“Temp”..它将返回 True ..这就是我的问题。 .
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);
}
当用户点击确认对话框中的 [Yes to all] 按钮时,YesToAll 设置为 true。
TransferType 只是用于标记项目是否要使用 SendMethod 或 ReceiveMethod
public enum TransferType
{
Send , Receive
};
那么我该如何解决这个问题.. 我应该使用自定义方法而不是 [Contains] 来检查名称和类型(文件或文件夹),因为每个项目已经有一个 subItem 来判断它是否是文件夹或文件
提前致谢。
【问题讨论】:
-
您需要添加另一个函数来获取类型(文件或文件夹),然后在同一个 if 语句中使用它。
-
@PraVn 每个项目都有 subItem[1] .. 如果它是一个文件,它将是 [15 M.B] 的大小 .. 如果它是一个文件夹,它将是 [Folder] .. 我的意思是我已经有项目类型。
-
@TimSchmelter 项目的类型是 ListViewItem :)
标签: c# listview contains listviewitem