Environment.SpecialFolder

A very useful environment enumeration is SpecialFolder. This provides an enumeration to special folders like My Pictures or the Startup menu. These folders are set by default by the system, or explicitly by the user. For example, ever try to get at a list of files contained in somebody’s MyPicture directory. Well using the SpecialFolder you can do this with the following code.

Imports System.io

Public Class Form1

 

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim theFiles As String()

        theFiles = Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "*", SearchOption.AllDirectories)

        Dim i As Integer

        For i = 0 To (theFiles.Length - 1)

            MsgBox(theFiles(i).ToString())

        Next

    End Sub

End Class

Or the list of files in the user’s startup menu

Imports System.io

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim theFiles As String()

        theFiles = Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.StartMenu), "*", SearchOption.AllDirectories)

        Dim i As Integer

        For i = 0 To (theFiles.Length - 1)

            MsgBox(theFiles(i).ToString())

        Next

    End Sub

End Class