Click here to Skip to main content
15,886,689 members
Articles / Desktop Programming / Win32

Accessing All of Windows Special Folders

Rate me:
Please Sign up or sign in to vote.
4.96/5 (14 votes)
12 Mar 2008CPOL10 min read 102.4K   4.5K   48  
Get the path names to Windows special folders; optionally create the folders if they are missing; access physical and virtual special folders.
Imports ZED.WinAPI.SHELL32

''' <copyright>
'''###########################################################################
'''##                Copyright (c) 2008 Warrick Procter.                    ##
'''##                                                                       ##
'''## This work is covered by the "Code Project Open License", a copy of    ##
'''## which is enclosed with this package as:                               ##
'''##         "Code Project Open License (CPOL).txt",                       ##
'''## and is available from http://www.codeproject.com/.                    ##
'''##                                                                       ##
'''## No other use is permitted without the express prior written           ##
'''## permission of Warrick Procter.                                        ##
'''## For permission, try these contact addresses (current at the time of   ##
'''## writing):                                                             ##
'''##     procter_AT_xtra_DOT_co_DOT_nz                                     ##
'''##     The address for service of company "ZED Limited", New Zealand.    ##
'''###########################################################################
''' </copyright>
''' <disclaimer>
'''###########################################################################
'''## REPRESENTATIONS, WARRANTIES AND DISCLAIMER                            ##
'''## ------------------------------------------                            ##
'''## THIS WORK IS PROVIDED "AS IS", "WHERE IS" AND "AS AVAILABLE", WITHOUT ##
'''## ANY EXPRESS OR IMPLIED WARRANTIES OR CONDITIONS OR GUARANTEES. YOU,   ##
'''## THE USER, ASSUME ALL RISK IN ITS USE, INCLUDING COPYRIGHT             ##
'''## INFRINGEMENT, PATENT INFRINGEMENT, SUITABILITY, ETC. AUTHOR EXPRESSLY ##
'''## DISCLAIMS ALL EXPRESS, IMPLIED OR STATUTORY WARRANTIES OR CONDITIONS, ##
'''## INCLUDING WITHOUT LIMITATION, WARRANTIES OR CONDITIONS OF             ##
'''## MERCHANTABILITY, MERCHANTABLE QUALITY OR FITNESS FOR A PARTICULAR     ##
'''## PURPOSE, OR ANY WARRANTY OF TITLE OR NON-INFRINGEMENT, OR THAT THE    ##
'''## WORK (OR ANY PORTION THEREOF) IS CORRECT, USEFUL, BUG-FREE OR FREE OF ##
'''## VIRUSES. YOU MUST PASS THIS DISCLAIMER ON WHENEVER YOU DISTRIBUTE THE ##
'''## WORK OR DERIVATIVE WORKS.                                             ##
'''###########################################################################
''' </disclaimer>
''' <history>
''' 2008-03-12 [Warrick Procter] Created.
''' </history>
''' <summary>
''' Special Folders form.
''' </summary>
''' <overview>
''' </overview>
''' <remarks>
''' </remarks>
''' <notes>
''' </notes>
Public Class Form1

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim xInt32 As Int32
    Dim xPF As enuCSIDLPhysical
    Dim xVF As enuCSIDLVirtual
    Dim xStr As String

    Me.RichTextBox1.SelectedText = "SF#  " + "Special Folder Name".PadRight(25) + "Path" + vbCrLf
    Me.RichTextBox1.SelectedText = "---- " + "-".PadRight(24, "-"c) + " ".PadRight(76, "-"c) + vbCrLf
    Me.RichTextBox1.ScrollToCaret()
    Me.RichTextBox1.Refresh()

    For xInt32 = 0 To &HFF

      Try

        xPF = DirectCast(xInt32, enuCSIDLPhysical)
        xStr = GetSpecialFolderPath(xPF)

        If xStr <> "" Then

          Me.RichTextBox1.SelectedText = xInt32.ToString("000") + ") " _
                + xPF.ToString.PadRight(25) + xStr + vbCrLf
          Me.RichTextBox1.ScrollToCaret()
          Me.RichTextBox1.Refresh()

        ElseIf System.Enum.IsDefined(GetType(enuCSIDLPhysical), xInt32) Then

          Me.RichTextBox1.SelectedText = xInt32.ToString("000") + ") " _
                + xPF.ToString.PadRight(25) + "        ==> Physical Folder, not created." + vbCrLf
          Me.RichTextBox1.ScrollToCaret()
          Me.RichTextBox1.Refresh()

        ElseIf System.Enum.IsDefined(GetType(enuCSIDLVirtual), xInt32) Then

          xVF = DirectCast(xInt32, enuCSIDLVirtual)
          Me.RichTextBox1.SelectedText = xInt32.ToString("000") + ") " _
                + xVF.ToString.PadRight(25) + "        ==> Virtual Folder, does not exist." + vbCrLf
          Me.RichTextBox1.ScrollToCaret()
          Me.RichTextBox1.Refresh()

        End If

      Catch ex As Exception

        Me.RichTextBox1.SelectedText = xInt32.ToString("000") + ") " _
              + "(We got an eror with this value.)" + vbCrLf
        Me.RichTextBox1.ScrollToCaret()
        Me.RichTextBox1.Refresh()

      End Try

    Next

  End Sub
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
ZED
New Zealand New Zealand
A DEC PDP/11 BasicPlus2 developer from the 80s.

Comments and Discussions