Search code examples
vb.netwindowsenvironment-variablesopenfiledialog

How to reference the current Windows user's video folder path in VB.net


I am looking for a way to reference the current user's "MyVideos" folder in VB.NET.

I goal is to use this reference to set the InitialDirectory propery of my OpenFileDialog object. omething lie this:

OpenFileDialog1.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments

under SpecialDirectories, I cannot find a property for MyVideos. The only properties I have under SpecialDirectories are:

.Desktop
.MyDocuments
.MyMusic
.MyPictures
.Programfiles
.Programs
.Temp

Am I missing something? Is there another way to access this information?

I am able to get the user's root folder, and combine it with "Videos", like this:

Dim vidPath As String = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Videos")

However, that assumes the user has not changed the location of their My Videos folder in it's location properties.

My Videos Folder - Location Properties

I would like to come up with a method to reference this location, in case the user has changed this location setting.


Solution

  • The missing folders in the Environment.SpecialFolder enumeration are available via an API call. There are several C# answers for this, mostly partials (get a specific folder). VB version for all (?) the missing ones:

    Public Partial Class NativeMethods
    
        <DllImport("shell32.dll")>
        Private Shared Function SHGetKnownFolderPath(<MarshalAs(UnmanagedType.LPStruct)> 
                                                rfid As Guid,
                                                dwFlags As UInt32,
                                                hToken As IntPtr,
                                                ByRef pszPath As IntPtr) As Int32
        End Function
    
       ' in order, below are:
        Public Enum ShellSpecialFolders
            Contacts
            Downloads
            Links
            Music
            Pictures
            SavedGames
            SavedSearches
            Videos
        End Enum
    
        Private Shared ShellFolderGuids As Guid() = {
                            Guid.Parse("{56784854-C6CB-462B-8169-88E350ACB882}"),
                            Guid.Parse("{374DE290-123F-4565-9164-39C4925E467B}"),
                            Guid.Parse("{BFB9D5E0-C6A9-404C-B2B2-AE6DB6AF4968}"),
                            Guid.Parse("{4BD8D571-6D19-48D3-BE97-422220080E43}"),
                            Guid.Parse("{33E28130-4E1E-4676-835A-98395C3BC3BB}"),
                            Guid.Parse("{4C5C32FF-BB9D-43B0-B5B4-2D72E54EAAA4}"),
                            Guid.Parse("{7D1D3A04-DEBB-4115-95CF-2F29DA2920DA}"),
                            Guid.Parse("{18989B1D-99B5-455B-841C-AB7C74E4DDFC}")
                                             }
    
        Friend Shared Function GetSpecialFolder(folder As ShellSpecialFolders) As String
            Dim ret As Int32
            Dim fPath As IntPtr
            ' == "Dont Vertify" flag:
            Dim SHFlag As UInt32 = &H4000
    
            ret = SHGetKnownFolderPath(ShellFolderGuids(folder), SHFlag,
                                       New IntPtr(0), fPath)
    
            If ret = 0 Then
                Return Marshal.PtrToStringUni(fPath)
            Else
                Return ""
            End If
        End Function
    
        ' Optional single purpose version
        Friend Shared Function GetSpecialVideoFolder() As String
            Return GetSpecialFolder(ShellSpecialFolders.Videos)
        End Function
    '...
    End Class
    

    Example usage:

    spath = NativeMethods.GetSpecialFolder(NativeMethods.ShellSpecialFolders.Videos)
    Console.WriteLine("Videos are in: {0}", spath)
    

    Or if you want to write wrappers for them:

    spath = NativeMethods.GetSpecialVideoFolder()
    

    If you want to get the default folders (rather than C:\Users\USER NAME\... you'd get C:\Users\Default\...) change the IntPtr param to -1:

    ret = SHGetKnownFolderPath(ShellFolderGuids(folder), SHFlag,
                                   New IntPtr(-1), fPath)
    

    Results:

    enter image description hereenter image description here

    Note: The folder returned apparently need not exist. Especially using the Default version, several folders returned do not actually exist on my system.