borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (Followup: Add data in VB .Net)Next Topic (Disable And Enable Win Key) New Topic New Poll Post Reply
AndreaVB Forum : VB.Net : how to delete a folder or file in a directory?
Poster Message
tomato82
Level: Protégé

Registered: 06-06-2005
Posts: 4

icon how to delete a folder or file in a directory?

Hello there!
I need to delete a folder or a file in a directory using vb.net. The folder and file will be deleted when this file is added to another folder. Therefore, the existing file will be deleted from the old folder. And the old folder will be deleted if the file is the last file in that folder.

Thanks alot for your help.

06-06-2005 at 04:18 AM
View Profile Send Email to User Show All Posts | Quote Reply
Goran
Level: Moderator

Registered: 16-05-2002
Posts: 1681
icon Re: how to delete a folder or file in a directory?

Directory Class: System.IO Namespace

method Exists - first check if directory exists
mothid Delete - delete directory.


____________________________
If you find the answer helpful, please mark this topic as solved.

06-06-2005 at 01:58 PM
View Profile Send Email to User Show All Posts | Quote Reply
tomato82
Level: Protégé

Registered: 06-06-2005
Posts: 4
icon Re: how to delete a folder or file in a directory?

here are the codes i use, but it does not work.
Can i know which part is the problem? thanks alot.

  Public Function DeleteFile(ByVal strPath As String) As Boolean
        Dim objDir As New DirectoryInfo(strPath)

        Try
            objDir.Delete(True)
            Return True
        Catch
            Return False
        End Try
    End Function

08-06-2005 at 07:01 AM
View Profile Send Email to User Show All Posts | Quote Reply
Goran
Level: Moderator

Registered: 16-05-2002
Posts: 1681
icon Re: how to delete a folder or file in a directory?

YOu need to write what error does it raise (remove Try... End Try - or catch an excpetion and display it)m otherwise we cant help you. The code you posted looks fine.

____________________________
If you find the answer helpful, please mark this topic as solved.

08-06-2005 at 01:31 PM
View Profile Send Email to User Show All Posts | Quote Reply
kimsea
Level: Trainee


Registered: 06-10-2006
Posts: 3
icon Re: how to delete a folder or file in a directory?

Import System.IO

Try
    Driectory.Delete("C:\testDir")
Catch ex1 as IOException
    If MessageBox.Show("Directory is not empty," _
       & " delete sub directories too?", "", _
       MessageBoxButtons.YesNo, _
       MessageBoxIcon.Question) = DialogResult.Yes then
      
       ' Delete directory and all contents
       Directory.delete("C:\TestDir",True)
    End If
End Try

06-10-2006 at 09:10 AM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : VB.Net : how to delete a folder or file in a directory?
Previous Topic (Followup: Add data in VB .Net)Next Topic (Disable And Enable Win Key) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder