如何删除VB.NET中列表框中的所有文件 [英] How do I delete all files in a listbox in VB.NET
本文介绍了如何删除VB.NET中列表框中的所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我有一个包含文件路径的列表框,在表单加载时它会选择所有项目。所以我的问题是如何通过点击按钮删除所有列表框路径中的实际文件。
我尝试过:
File.Delete(ListBox1.SelectedItems)并且这个没有工作,下面有一条红线。
so i have a listbox with file paths and on form load it selects all items. So my question is how do i delete the actual files from all the listbox paths by a button click.
What I have tried:
File.Delete(ListBox1.SelectedItems) and this didnt work just had a red line underneath.
推荐答案
File.Delete一次删除一个文件:您需要做的就是在SelectedItems属性上使用 For Each
循环,将它返回的对象强制转换为字符串,并删除该文件。
File.Delete deletes a single file at a time: all you need to do is use a For Each
loop on the SelectedItems property, cast the object it returns to a string, and delete that file.
这里是您要求的代码
here is the code which you have asked for
private void button1_Click(object sender, EventArgs e)
{
foreach (string path in listBox1.SelectedItems)
{
System.IO.File.Delete(path);
}
}
参考
ListBox.SelectedItems Property(System.Windows.Forms) [ ^ ]
File.Delete Method(String)(System.IO) [ ^ ]
这篇关于如何删除VB.NET中列表框中的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文