0

I am trying to make a quick link combobox in vb. But I can't seem to get it to open the Pictures, Music, Videos & Downloads folder.

I tried Shell("explorer %HOMEPATH%/pictures", AppWinStyle.NormalFocus) but that just opens the documents folder, Any help?

HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
Nioxed
  • 3
  • 2

2 Answers2

0

Here you go...

'Open Pictures folder
Process.Start(My.Computer.FileSystem.SpecialDirectories.MyPictures)
'Open Music folder
Process.Start(My.Computer.FileSystem.SpecialDirectories.MyMusic)
'Open Videos folder
Dim strVideosPath As String = System.Environment.GetFolderPath(Environment.SpecialFolder.MyVideos)
Process.Start(strVideosPath)
'Open Downloads folder
Dim strDownloadsPath As String = System.Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Downloads"
Process.Start(strDownloadsPath)
SSS
  • 4,807
  • 1
  • 23
  • 44
0

If you want to reliably get the Downloads folder (which might have a different name from "Downloads", especially in different language versions fo Windows, and a path which is not even on the same drive as the "user profile"), you will need to ask shell32 where it is:

Imports System.Runtime.InteropServices
Imports System.Text

Public Class OtherSpecialFolder

    ' Documentation of Common HRESULT Values:
    ' https://msdn.microsoft.com/en-us/library/windows/desktop/aa378137%28v=vs.85%29.aspx

    Const S_OK As Integer = 0
    Const E_FAIL As Integer = &H80004005
    Const E_INVALIDARG As Integer = &H80070057

    <DllImport("shell32.dll", CharSet:=CharSet.Auto)>
    Private Shared Function SHGetKnownFolderPath(ByRef id As Guid, flags As Integer, token As IntPtr, ByRef path As StringBuilder) As Integer
        ' Documentation of SHGetKnownFolderPath function:
        ' https://msdn.microsoft.com/en-us/library/windows/desktop/bb762188%28v=vs.85%29.aspx
    End Function

    Public Shared Function GetDownloadsFolder() As String

        ' Known folder GUIDs at:
        ' https://msdn.microsoft.com/en-us/library/windows/desktop/dd378457%28v=vs.85%29.aspx

        Dim FOLDERID_Downloads As Guid = New Guid("374DE290-123F-4565-9164-39C4925E467B")
        Dim sb As New StringBuilder

        Dim hresult As Integer = SHGetKnownFolderPath(FOLDERID_Downloads, 0, IntPtr.Zero, sb)

        Select Case hresult
            Case S_OK
                Return sb.ToString()
            Case E_FAIL
                Throw New ArgumentException(String.Format("KNOWNFOLDERID GUID {0} (Downloads) does not have a path.", FOLDERID_Downloads))
            Case E_INVALIDARG
                Throw New ArgumentException(String.Format("Known folder with GUID {0} (Downloads) is not present on the system.", FOLDERID_Downloads))
            Case Else
                Throw New Exception(String.Format("GetDownloadsFolder function failed, unexpected HRESULT = 0x{0:X}.", hresult))
        End Select

    End Function

End Class

The SHGetKnownFolderPath function may only be available on Windows Vista and later.

Usage:

Dim downloadsFolder As String = OtherSpecialFolder.GetDownloadsFolder()

Ref: Downloads folder: not special enough?

Community
  • 1
  • 1
Andrew Morton
  • 24,203
  • 9
  • 60
  • 84