locked
Just a little doubt about deleting files from hard disk with a checkboxlist RRS feed

  • Question

  • User17798125 posted

    Forum : Hi there, I just want a little help about deleting files from the hard disk with a checkboxlist. I have a little code but it do not have function I mean it does not work ... I leave the VB code, the delete action is what I can not make it work.

    Thanks and hope you will help me.


    Protected Sub btn2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn2.Click
            Dim li As ListItem
            Dim del As String = "C:\Test\" & use.Text
            For Each li In cb.Items
                If li.Selected Then
                    Try
                        File.Delete((del) & li.Selected)
                        lb.Text = "Archivo Eliminado"
                    Catch
                        btn.Visible = False
                        lb2.Text = "NADA"
                        btn2.Visible = False
                        btn3.Visible = False

                    End Try
                End If

            Next
        End Sub

    Monday, March 29, 2010 11:09 PM

Answers

  • User17798125 posted

      

      Hi everyone !!!   well I just solve this problem and the solution is rare and simple, here is the code :


      Protected Sub btn2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn2.Click
            Dim li As ListItem
            Dim del As String = "C:\Test\" & use.Text
    
            For Each li In cb.Items
                If li.Selected Then
                    Try
                        File.Delete(del & "\" & li.Text)
                        lb.Text = "Archivo Eliminado"
                        Dim i As Integer
                        
    
                    Catch
                        btn.Visible = False
                        lb2.Text = "NADA"
                        btn2.Visible = False
                        btn3.Visible = False
                    End Try
                End If
            Next
        End Sub


    Where File.Delete(del & "\" & li.text   ----   I had  li.selected and this is wrong. The correct attribute is li.text 
    and now i can delete the files selected from the CBList.

    Thanks to all for answers. Take care !!
    !
     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 4, 2010 7:55 PM

All replies

  • User-821857111 posted

    You need to see if there is an error (permission, perhaps?), by removing the try catch, or putting a breakpoint on the File.Delete() line and hitting F10 when it stops there.

    Probably simpler just to run this code and see if you get an error page:

    Protected Sub btn2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn2.Click
            Dim li As ListItem
            Dim del As String = "C:\Test\" & use.Text
            For Each li In cb.Items
                If li.Selected Then
                        File.Delete((del) & li.Selected)
                End If
            Next
        End Sub


    Tuesday, March 30, 2010 12:53 AM
  • User197322208 posted

    Catch
                        btn.Visible = False
                        lb2.Text = "NADA"
                        btn2.Visible = False
                        btn3.Visible = False

                    End Try

    Catch
                        btn.Visible = False
                        lb2.Text = "NADA"
                        btn2.Visible = False
                        btn3.Visible = False


    Response.Write(ex.Message)
                    End Try


    I'll bet that the message is something with SecurityException

    Tuesday, March 30, 2010 12:54 AM
  • User-821857111 posted

    Response.Write(ex.Message)

    :o)

    or

    Catch
                        btn.Visible = False
                        lb2.Text = ex.Message
                        btn2.Visible = False
                        btn3.Visible = False

    End Try

    But I like the YSOD method ;o)


    Tuesday, March 30, 2010 1:05 AM
  • User17798125 posted


         Hi there Thanks for all the answers but still have no functions about the deleting files, used all your suggestions with some combinations and nothing ... any other idea ????

        I tried to delete a single file with this and it works, but this only delete the file that I describe in the instruction, I need to select diferent files from the checklistbox and delete the selected files, thats what I can not make it work .... Cry

         Protected Sub btn2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn2.Click
            Dim li As ListItem
            For Each li In cb.Items
                If li.Selected Then
                        File.Delete("C:\Test\one.docx")
                End If
            Next
        End Sub
            

              Thanks !!

    Tuesday, March 30, 2010 2:48 PM
  • User17798125 posted


         Hi there Thanks for all the answers but still have no functions about the deleting files, used all your suggestions with some combinations and nothing ... any other idea ????

        I tried to delete a single file with this and it works, but this only delete the file that I describe in the instruction, I need to select diferent files from the checklistbox and delete the selected files, thats what I can not make it work .... Cry

         Protected Sub btn2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn2.Click
            Dim li As ListItem
            For Each li In cb.Items
                If li.Selected Then
                        File.Delete("C:\Test\one.docx")
                End If
            Next
        End Sub
            

              Thanks !!

    Tuesday, March 30, 2010 2:49 PM
  • User1485408165 posted

    Protected Sub btn2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn2.Click
            Dim li As ListItem
            Dim del As String = "C:\Test\" & use.Text
            For Each li In cb.Items
                If li.Selected Then
                    Try
                        File.Delete((del) & li.Selected)
                        lb.Text = "Archivo Eliminado"
                    Catch
                        btn.Visible = False
                        lb2.Text = "NADA"
                        btn2.Visible = False
                        btn3.Visible = False

                    End Try
                End If

            Next
        End Sub

     

    Your problem might be something as simple as missing a "\"

     

    Dim del As String = "C:\Test\" & use.Text

    if use.Text = "Happy Fun File Deletion Folder"

    and li.Selected = "HappySounds.mp3"

    Then File.Delete((del) & li.Selected) is going to look like this

     

    File.Delete("C:\Test\Happy Fun File Delection FolderHappySounds.mp3" )

     

    You might want to check for a \ at the start of li.selected or a \ at the end of del

     

    Hope this helps.

    Friday, April 2, 2010 11:56 AM
  • User17798125 posted

    Hi torren: Thanks for your answer and I tried it with many combinations and sorry but it does not work at this time, Im still try now but sure I miss something I have the next code:

     

    Protected Sub btn2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn2.Click
            Dim li As ListItem
            Dim del As String = "C:\Test\" & use.Text
            For Each li In cb.Items
                If li.Selected Then
                    Try
                        File.Delete(del & "\" & li.Selected)
                        lb.Text = "Archivo Eliminado"
                    Catch
                        btn.Visible = False
                        lb2.Text = "NADA"
                        btn2.Visible = False
                        btn3.Visible = False
                    End Try
                End If
            Next
        End Sub

    Protected Sub btn2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn2.Click
            Dim li As ListItem
            Dim del As String = "C:\Test\" & use.Text
            For Each li In cb.Items
                If li.Selected Then
                    Try
                        File.Delete(del & "\" & li.Selected)
                        lb.Text = "Archivo Eliminado"
                    Catch
                        btn.Visible = False
                        lb2.Text = "NADA"
                        btn2.Visible = False
                        btn3.Visible = False
    
                    End Try
                End If
    
            Next
        End Sub



    As you can see, I put the missing "\" inside the file.delete instruction ... but so, I put it in the Dim del declaration as follows "C:\Test\" & use.Text & "\" ..... and nothing happened.

    Hope you help me because what you said to me make sense. Greetings. 

    Saturday, April 3, 2010 2:08 PM
  • User-821857111 posted

    You really need to change this:

    1.                 Catch  
    2.                     btn.Visible = False  
    3.                     lb2.Text = "NADA"  
    4.                     btn2.Visible = False  
    5.                     btn3.Visible = False 

    to this (changes in bold):

    1.                 Catch  Ex As System.Exception
    2.                     btn.Visible = False  
    3.                     lb2.Text = Ex.Message
    4.                     btn2.Visible = False  
    5.                     btn3.Visible = False 

    And then tell us what appears in the label when you run your code.


    Saturday, April 3, 2010 2:36 PM
  • User17798125 posted

      

      Hi everyone !!!   well I just solve this problem and the solution is rare and simple, here is the code :


      Protected Sub btn2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn2.Click
            Dim li As ListItem
            Dim del As String = "C:\Test\" & use.Text
    
            For Each li In cb.Items
                If li.Selected Then
                    Try
                        File.Delete(del & "\" & li.Text)
                        lb.Text = "Archivo Eliminado"
                        Dim i As Integer
                        
    
                    Catch
                        btn.Visible = False
                        lb2.Text = "NADA"
                        btn2.Visible = False
                        btn3.Visible = False
                    End Try
                End If
            Next
        End Sub


    Where File.Delete(del & "\" & li.text   ----   I had  li.selected and this is wrong. The correct attribute is li.text 
    and now i can delete the files selected from the CBList.

    Thanks to all for answers. Take care !!
    !
     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 4, 2010 7:55 PM