Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VB.net Delete  all files from a folder

Posted on 2014-07-27
4
Medium Priority
?
1,070 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:Murray Brown
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 64

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 2000 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:Murray Brown
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

715 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