文件未删除?请帮忙 [英] File Not Deleted? Please Help

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

问题描述

我现在已经多次发布了这个问题,并给出了一些答案,这些答案使我在这种情况下取​​得了任何进展.我的程序扫描文本文件中的名称,如果找到了文本文件中的名称,则结果将成为我的复选框中的一项.问题在于我的代码仅删除了该项目,而没有删除计算机中的实际文件.


我的代码如下:

I have posted this question now a few times with few answers that have given me any progress in this situation. My program scans for names that are within a text file and if the name in the text file is found the result then becomes a item in my checklistbox. The problem is that my code only removes the item and not the actual file in my computer.


my code is as follows:

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
For i As Integer = CheckedListBox1.CheckedItems.Count - 1 To 0 Step -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.Information)
         CheckedListBox1.Items.Remove(CheckedListBox1.CheckedItems)
                End If
            Catch ex As Exception
            End Try
        Next
end sub





再次有人说我应该通过说System.io.file.exists(red)然后进入system.io.file.delete(red)来查看文件是否存在,如果为true,则删除或显示msgbox错误.

这是个好主意,我尝试将代码更改为此.....





Again I have had someone say that I should see if the file exists by saying System.io.file.exists(red) and then going onto system.io.file.delete(red) and if true to delete or show a msgbox error.

This is a good idea and I have tried to change the code to this.....

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




这仍然只能执行第一个示例所执行的操作,实际上,我对为什么仍不删除文件的想法或想法不了解.请问有人可以查看此代码,并可能提供他或她重写的代码以成功执行文件删除操作,因为我已经尝试了大约一个星期,但仍未完全回答3个以上的问题.

感谢您尝试提供的帮助,在此问题上,我将提供任何帮助.....




This still only does what the first example does and truly I am out of ideas or thoughts of why the file is still not being deleted. Will someone please review this code and possibly give his or her rewritten code to perform the file delete successfully as I have been trying for about a week with more than 3 questions not fully answered.

Thank you for trying to help I appriciate whatever help you give in this issue.....

推荐答案

如前所述,您无法删除类似的内容循环播放.

http://generally.wordpress.com/2007/10/24/removing-items-in-a-list-while-iterating-through-it [
As told before, you can''t delete items like that in a loop.

http://stackoverflow.com/questions/380451/c-how-do-i-do-i-loop-through-items-in-a-list-box-and-then-remove-that-item[^]

http://generally.wordpress.com/2007/10/24/removing-items-in-a-list-while-iterating-through-it[^]

Also, check the value of red. Is this the actual filename? Check the value of red when setting it:
red = Replace(CheckedListBox1.CheckedItems.Item(i).ToString(), "Threat Found: ","")



否则,请提供有关列表框中信息的更多信息.您可以添加填充它的代码,以便也可以检出.



Otherwise give more info on the information that is in the listbox. You could add the code that fills it so that can also be checked out.


我首先建议,空的try catch块将再次出现在后面,以叮咬您.如果系统在删除文件时遇到问题,它将引发异常,告诉您原因,但是您将其吸收到空的try catch中,这只会使它消失.
可能是该文件正在由系统上的其他资源使用.如果是这种情况,我认为您将无法像这样强制删除它.查看抛出的异常(如果有),并使用此异常(
MSDN文档)查看正在发生的事情.
I would suggest first that the empty try catch block is going to come back to bite you in the backside. If the system experiences problems deleting the file it will throw an exception telling you why, but you are absorbing it in the empty try catch which simply makes it go away.
It could be that the file in question is in use by some other resource on the system. If this is the case, I don''t think you''ll be able to force delete it like this. Take a look at the exception thrown(if there is one) and use this (MSDN Documentation) to see what is happening.


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

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