Solved

VB.net Delete  all files from a folder

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

760 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