Solved

VB.net Delete  all files from a folder

Posted on 2014-07-27
4
947 Views
Last Modified: 2014-07-27
Hi
I am using the following code to delete all the files from a folder but it doesn't seem to work

        Dim MYDOC_DIR As String = Environ("userprofile") & "\My Documents"
        Dim TEMP_IMAGES As String = MYDOC_DIR & "\Temp_Images"

        Dim myDirInfo As System.IO.DirectoryInfo = New DirectoryInfo(TEMP_IMAGES)

        For Each file As FileInfo In myDirInfo.GetFiles()
            file.Delete()
        Next
        For Each dir As DirectoryInfo In myDirInfo.GetDirectories()
            dir.Delete(True)
        Next
0
Comment
Question by:murbro
4 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40222937
So what does happen? Do you get an exception thrown, or something else?
0
 
LVL 15

Expert Comment

by:Ess Kay
ID: 40223050
sounds like you don't have permission to delete the user profile
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 40223407
Do not use stuff like Environ("userprofile") & "\My Documents" when you want to access a standard directory. There are too many variations between different versions of Windows, different languages, and the possibility for a user to change the name or location of a standard directory.

It is safer to use the following instead. It will give you the right location no matter the situation:

System.Environment.GetFolderPath(environment.SpecialFolder.MyDocuments)

Note that SpecialFolders provides a list of all the standard directories.
0
 

Author Closing Comment

by:murbro
ID: 40223529
Thanks very much James. That did the trick. I have used the previous code all over my apps so glad that you short circuited future user errors.
Murray
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question