本文介绍了如何使用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?

推荐答案

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



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

10-23 08:57