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
Solved

VB.net Delete  all files from a folder

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

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

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…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

856 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