查找文件并删除代码拆分帮助 [英] Find file and delete code split Help

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

问题描述

我试图以两种方式放置此代码以使其起作用.
1)找到文件并将其添加到我的复选框.
2)使用删除按钮删除找到的文件
如上所述,这是要分成两部分的代码ID:

I am trying to place this code to function in two ways.
1) Find the file and add it to my checklistbox.
2) Use a delete button to delete the found file
This is the code id like to split into two sections as stated above:

 Function RepeatFiles(ByVal dirs As DirectoryInfo)
Dim fileListing() As FileInfo = dirs.GetFiles()   
If fileListing.Length > 0 Then
      For Each fl As FileInfo In fileListing         
      If Button1.Enabled = False Then
      currentPath.Text = fl.FullName            
      Application.DoEvents()            
      Dim currentFileName As String = fl.Name
Dim fileExists As Boolean = My.Resources.VirusList.Contains (currentFileName)            
      If fileExists = True Then
If MessageBox.Show("Infection found:" & fl.Name,"Threat Alert!",MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
CheckedListBox1.Items.Add(String.Format("Infection Removed:{0}{1}", fl.FullName, fl.Name))                          
      fl.Delete()                       
                        End If
                   End If
               End If
           Next
       End If


当前,此代码在每次发现威胁时都会停止扫描,并要求用户删除文件或不删除文件.
这可能有点烦人,而不仅仅是将所有威胁添加到复选框中,并在扫描完成后提示您采取措施.
这就是我想要执行的操作,以便用户可以选中或取消选中要删除的威胁. .

请仅发布解决方案,而不要发布解决方案,因为我已经准备就绪,这就是为什么我在这里首先提出问题的原因.预先感谢您的支持:)


Currently this code stops the scan every time a threat is found and asks the user to either delete the file or not.
This can be a bit of a annoyance instead of just adding all the threats to the checklistbox and prompting for action when the scan has completed.
This is what I''d like it to do so that the user can check or uncheck the threats to delete. .

Please only post the solution and not how to get the solution because I have all ready tried and is why Im here to ask the question in the first place. thank you in advance for all of your support :)

推荐答案

戴尔,

如果您使用的是上面的代码,那么肯定可以确定如何将文件添加到字符串列表或字符串列表项集合中,然后在扫描完成时调用另一个函数来处理删除位.

您说:请仅发布解决方案,而不是如何获取解决方案",那不是它在代码项目上的工作方式…….
Dale,

If you are using the code above, then surely you can work out how to add the files to a list<of String) or listview items collection and then when the scan finishes call another function to handle the delete bit.

You said: "Please only post the solution and not how to get the solution", thats not how it works on code project.......


Dale,

这是一个非常快速且粗糙的代码转储;
我在详细信息"视图中有一个带有列表视图的表单,已启用复选框和1列.还有获取文件",停止"和删除文件"按钮;

还有一个文本框,其中包含"C:\"作为起点.

该代码将对所有文件进行递归搜索,直到您按下stop为止.

找到它们后,它们将被添加到列表视图中.

然后,用户可以在复选框上选择要删除的文件.

当按下删除按钮时,将提示用户确认删除所选文件.如果按是,则将删除每个文件.

Dale,

Here is a very quick and rough code dump;
I have a form with a listview in Details view, check boxes enabled and 1 column. Also a Get File, Stop and Delete files buttons;

There is also a text box which contains "C:\" used as the start point.

The code will do a recursive search for all files until you press stop.

These will be added to the listview as they are found.

The user can then select on the check boxes the files to delete.

When the delete button is pressed, the user is prompted to confirm deletion of selected files. If yes is pressed then each file is deleted.

Imports System.IO

Public Class Form1
Private StopSearch As Boolean = False

Private Sub getFiles(ByVal Path As String)

    If StopSearch Then Exit Sub

    Dim subDirs As New List(Of String)
    Dim files As New List(Of String)

    Try
        subDirs = Directory.GetDirectories(Path).ToList
        'If the GetDirs fails no need to try get files, this will be skipped
        files = Directory.GetFiles(Path).ToList
    Catch ex As Exception
        Debug.WriteLine("DIR Error: " + ex.Message)
    End Try

    For Each item As String In files
        ListViewFiles.Items.Add(item)
        Application.DoEvents()      'Only for example, in reality use background workers.
    Next

    For Each item As String In subDirs
        getFiles(item)
    Next

End Sub


Private Sub ButtonGetFiles_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonGetFiles.Click
    StopSearch = False
    getFiles(TextBoxStartPath.Text)
End Sub


Private Sub ButtonDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonDelete.Click
    If ListViewFiles.CheckedIndices.Count > 0 Then
        If MessageBox.Show("Delete " + ListViewFiles.CheckedIndices.Count.ToString + " file(s)?", "Delete Files", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
            For Each item In ListViewFiles.CheckedIndices
                Try
                    Dim listitem As ListViewItem = ListViewFiles.Items(item)
                    File.Delete(listitem.Text)
                Catch ex As Exception
                    MsgBox("Error deleting file: " + ex.Message)
                End Try
            Next
        End If
    End If
End Sub

Private Sub ButtonStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonStop.Click
        StopSearch = True
End Sub
End Class


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

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