简体   繁体   English

c#如何将selectedItems(listview)复制到变量中,然后在ListView中使用它(复制和粘贴)FileExplorer

[英]c# how to copy the selectedItems (listview) into a variable then use it later (copy & paste) FileExplorer in ListView

im making a file explorer tool in c# .. im using ListView to explore files 即时通讯使用ListView浏览文件,在c#中创建一个文件浏览器工具

but i have an issue when i copy items(files) 但我有一个问题,当我复制项目(文件)

public ListView.SelectedListViewItemCollection copiedItems;

private void btnCopy_Click(object sender, EventArgs e)
    {
        copiedItems = listView1.SelectedItems;
        infoLabel.Text = "Item(s) copied to clipboard.";
    }

private void Paste()
    {
        if (copiedItems != null)
        {
            foreach (ListViewItem item in copiedItems)
            {
                if (File.Exists(item.ToolTipText))
                {
                    if (MessageBox.Show(item.ToolTipText + "is already exists\r\nDo you want to overwrite it?"
                    , "Overwrite", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk) == DialogResult.OK)
                    {
                        File.Copy(item.ToolTipText, currAddress, true);
                        infoLabel.Text = "Item(s) Pasted.";
                    }
                    return;
                }
                File.Copy(item.ToolTipText, currAddress, false);
                infoLabel.Text = "Item(s) Pasted.";
            }
        }
    }

when i change the folder that i am into(currAddress) .. the paste doesn't work because the 当我更改我进入的文件夹(currAddress)..粘贴不起作用,因为

 ListView.SelectedListViewItemCollection copiedItems 

changed to null. 改为null。 .. here's my explorer code ..这是我的资源管理器代码

private void DirRecursive(string path)
    {
            DirectoryInfo dir = new DirectoryInfo(path);
            currAddress = path;
            txtAddress.Text = path;
            _iconListManager.ClearLists();
            listView1.Items.Clear();
            foreach (DirectoryInfo folder in dir.GetDirectories())
                {
                    if ((folder.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
                        continue;
                    ListViewItem item = new ListViewItem();
                    item.Text = folder.Name;
                    item.Tag = (object)folder.Parent;
                    item.ToolTipText = folder.FullName;
                    item.ImageIndex = _iconListManager.AddFolderIcon(folder.FullName);
                    listView1.Items.Add(item);
                }
                foreach (FileInfo file in dir.GetFiles())
                {
                    if ((file.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
                        continue;
                    ListViewItem item = new ListViewItem();
                    item.Text = file.Name;
                    item.Tag = (object)file.Directory.Parent;
                    item.ToolTipText = file.FullName;
                    item.ImageIndex = _iconListManager.AddFileIcon(file.FullName);
                    item.SubItems.Add(CnvrtUnit(file.Length));
                    listView1.Items.Add(item);
                }

is there any explanation why copiedItems changed to null and how can i fix it ? 有没有解释为什么copiedItems更改为null,我该如何解决?

thanks in advance :) 提前致谢 :)

I believe "listview1.SelectedItems" is just giving you a reference to the selected items. 我相信“listview1.SelectedItems”只是为您提供所选项目的参考。 This means that "copiedItems" points to the same object as "listview1.SelectedItems". 这意味着“copiedItems”指向与“listview1.SelectedItems”相同的对象。 This is why when you clear the listview1 items in DirRecursive you clear the copiedItems. 这就是为什么在清除DirRecursive中的listview1项时清除copiedItems的原因。 You need to get a separate copy of the items. 您需要获得项目的单独副本。 To do this you should probably use something like this: 要做到这一点,你应该使用这样的东西:

List<ListViewItem> copiedItems = new List<ListViewItem>();

foreach (ListViewItem item in listView1.Items)
{
    copiedItems.Add((ListViewItem) item.Clone());
}

Instead of 代替

public ListView.SelectedListViewItemCollection copiedItems;

use 采用

public List<string> copiedItems;

Change btnCopy_Click to this: btnCopy_Click更改为:

private void btnCopy_Click(object sender, EventArgs e)
    {
        copiedItems = listView1..SelectedItems.Cast<ListViewItem>().Select(li => li.ToolTipText).ToList();
        infoLabel.Text = "Item(s) copied to clipboard.";
    }

change Paste to this: Paste更改为:

private void Paste()
    {
        if (copiedItems != null)
        {
            foreach (string item in copiedItems)
            {
                if (File.Exists(item))
                {
                    if (MessageBox.Show(item + "is already exists\r\nDo you want to overwrite it?"
                    , "Overwrite", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk) == DialogResult.OK)
                    {
                        File.Copy(item, currAddress, true);
                        infoLabel.Text = "Item(s) Pasted.";
                    }
                    return;
                }
                File.Copy(item, currAddress, false);
                infoLabel.Text = "Item(s) Pasted.";
            }
        }
    }

The variable copiedItems isn't actually storing items, but rather storing a reference to where the selected items are. 变量copiedItems实际上并不存储项目,而是存储对所选项目所在位置的引用。 Or something to that effect. 或者那种效果。 If the same items are selected at the time of the paste, you will be ok. 如果在粘贴时选择了相同的项目,您将没问题。 But once the items are deselected in your list view, your copiedItems variable will now be an empty list. 但是,在列表视图中取消选择项后,您的copiedItems变量现在将成为空列表。 You need to explicitly copy the items that are selected when the copy button is pressed. 您需要显式复制按下复制按钮时选择的项目。 The following sample program illustrates what I'm talking about. 以下示例程序说明了我在说什么。 To make this run, create a new WinForms project, drag a ListView and two Button controls onto the form and replace the code in Form1.cs with the following. 若要进行此运行,请创建一个新的WinForms项目,将ListView和两个Button控件拖到窗体上,并使用以下内容替换Form1.cs的代码。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        ListView.SelectedListViewItemCollection items1;
        List<int> items2;

        public Form1()
        {
            InitializeComponent();
            items2 = new List<int>();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < 10; i++)
            {
                listView1.Items.Add(i.ToString());
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            items1 = listView1.SelectedItems;
            foreach (var item in listView1.SelectedItems)
            {
                ListViewItem lvItem =(ListViewItem)item; 
                items2.Add(int.Parse(lvItem.Text));
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {

        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM