如何使用checkedlistbox删除文件? [英] How do I delete files using checkedlistbox?

查看:84
本文介绍了如何使用checkedlistbox删除文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

图像我的表单

我正在开发类似防病毒程序的东西。我创建了tex文档。并且写了测试,123,asd。 (现在扫描它就像病毒一样。)

Image my form
I'm working on something like antivirus program. I created tex document. And Wrote test, 123, asd. (After the scan its like virus for now.)

string[] listName = new String[] { "test", "123", "asd" };



扫描后,在checkedlistbox上查看此文本文档。我想在checkedlistbox上选择并从我的电脑中删除。我尝试了以下代码。


After the scan this text document viewed on checkedlistbox. I want to select on checkedlistbox and delete from my computer. I tried the following code.

private void deleteViruses_btn(object sender, EventArgs e)
        {
            foreach (var item in checkedListBox1.CheckedItems.OfType<string>().ToList())
            {
                DialogResult dialogResult = MessageBox.Show("Do you want delete Founded Viruses?", "Delete", MessageBoxButtons.YesNo);
                if (dialogResult == DialogResult.Yes)
                {
                    File.Delete(item);
                    checkedListBox1.Items.Remove(item);
                }
            }
            MessageBox.Show("Deleted");
            checkedListBox1.Refresh();
            
        }



它不起作用。如何在选中checkedlistbox后删除此文件?


It does not work. How can I delete this files after selected checkedlistbox?

推荐答案

您的列表项应该是受感染文件的全名。您不必在UI中显示全名。为此,您可以使用工作所需的所有项目信息创建项类(或 struct )并覆盖 System.Object.ToString 。您将在我过去的答案中找到更多详细信息: combobox.selectedvalue在winform中显示{} [ ^ ] ,

Windows窗体列表框选择 [ ^ ]。



对于选定的项目,将每个项目都转换为 class / struct ,从适当的属性中提取文件名访问文件名,在 System.IO.File.Delete 中使用此值。







主要的错误是使用循环内的确认对话框。据我了解,要删除的文件的指示应该是项目的已检查状态。因此,您可以在循环之前使用对话框。



您需要在没有of type的情况下运行所有​​项目的循环,而不是to list(我总是使用 TreeView ,因为列表框的样式问题):
Your list items should be the full names of the infected files. You don't have to show the full names in UI. To achieve that, you can create an item class (or struct) with all item information you need for your work and override System.Object.ToString. You will find more detail in my past answers: combobox.selectedvalue shows {} in winform[^],
Windows Form ListBox Selections[^].

For selected items, typecast each to your class/struct, extract the file name from appropriate property used to access the file name, use this value in System.IO.File.Delete.



The major mistake is using the confirmation dialog inside the loop. As I understand, the indication of the file to be deleted should be the checked state of the item. So, you can use just on dialog before the loop.

You need to run the loop through all the items without "of type", without "to list" (I always use TreeView instead, due to style problems of the list box):
internal struct ItemHelper {
   internal ItemHelper(string fileName) { // for example
       this.FileName = fileName;
   }
   internal string FileName { get; private set; }
   public override string ToString() {
      return this.FileName; // for example
      // or System.IO.Path.GetFileName(this.FileName);
   } 
   //...
} 

// confirmation dialog here
for (object item in myCheckedListBox.CheckedItems) { // nothing else!
    // no need in dynamic cast:
    ItemHelper itemHelper = (ItemHelper)item;
    string fileName = itemHelper.FileName;
    System.IO.File.Delete(fileName);
    // IMPORTANT! do not delete the item! 
}

// delete all checked items here, separately



[结束编辑]



这就是全部。



-SA


某些代码下面有红线。表格中有一些错误。我修好了。它工作。谢谢。
There were red lines under the some codes. There were some errors in forms. And I fixed. It working. Thank you.


这篇关于如何使用checkedlistbox删除文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆