文件未删除 [英] File Not Deleted..

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

问题描述

我的删除按钮具有所有正确的功能,现在在msgbox中显示正确的消息,但是当我单击删除按钮时,即使在msgbox说文件已成功删除之后,我希望删除的文件仍保留在我的计算机上. ..我正在使用一个复选框,并希望使用删除按钮删除我选择的任何或所有选中的项目.


这是我的删除代码:

My delete button has all the right stuff and now displays the right message in the msgbox but when I click my delete button the file I wish to delete still remains on my computer even after the msgbox says the file has been successfully deleted... I''m using a checklistbox and would like the delete button to delete any or all of the checked items that I select.


Here is my delete code:

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        For i As Integer = 0 To CheckedListBox1.CheckedItems.Count - 1
            red = Replace(CheckedListBox1.CheckedItems.Item(i).ToString(), "Threat Found:","")
            Try
                System.IO.File.Delete(red)
                If Dir(red) <> "" Then
                MsgBox("Unable to delete" & red, MsgBoxStyle.Critical)
                Else
            MsgBox(String.Format("File {0} successfully deleted",red), MsgBoxStyle.Critical)
            CheckedListBox1.Items.Remove(CheckedListBox1.CheckedItems)
                End If
            Catch ex As Exception
            End Try
        Next
    End Sub

推荐答案

您应该执行从高到低的for循环:
You should do the for loop from high to low:
For i As Integer = CheckedListBox1.CheckedItems.Count - 1 to 0 Step -1


您正在删除项目,因此由于计数已更改,因此在循环结束时计数可能不正确.通过沿相反方向进行循环,可以避免此问题.

祝您好运!


You are deleting items and therefore the count can be incorrect at the end of the loop because the count has changed. By doing the loop in the opposite direction you avoid this problem.

Good luck!


0)好像您要删除文件名"Threat Found",因为您正在用该文本替换所选项目.

1)您在使用调试器吗?由于try/catch块,消息中的代码实际上在吞噬异常.

2)您不需要倒退SelectedItems数组,因为您没有从该数组中删除任何内容-您只是在更改文本(如果我了解您打算做什么) ).

3)列表框中显示的项目是否包含整个路径,还是仅文件名本身?如果只是文件名,则在尝试删除它时必须添加路径,以确保删除要删除的文件.
0) It looks as if you''re deleting the filename "Threat Found" because you''re replacing the selected item with that text.

1) Are you using the debugger? The code in your message is effectively eating the exception because of the try/catch block.

2) You shouldn''t need to step backwards through the SelectedItems array, because you''re not deleting anything from that array - you''re simply changing the text (if I understand what you''re intending to do).

3) Do the items shown in the listbox include the ENTIRE path, or is it just the file name itself? If it''s just the filename, you have to add the path to it when you try to delete it, just to make sure you''re deleting the file that you really want to delete.


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

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