Solved

VB.net Delete  all files from a folder

Posted on 2014-07-27
4
927 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

863 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now