שתף באמצעות


How to get the Downloads Directory

Question

Wednesday, October 18, 2017 4:09 PM

Hello anyone who is looking at this I have been trying for the past couple of days to get the downloads directory path (for visual basic) but nothing has been working.  What I want to do is move a file from the downloads directory to the program files directory does anyone know how I would do this.

Thx :)

All replies (6)

Wednesday, October 18, 2017 4:44 PM | 2 votes

Since Environment.SpecialFolders has limitations, many articles suggest SHGetKnownFolderPath. Try this console fragment:

Dim np As IntPtr
SHGetKnownFolderPath(New Guid("374DE290-123F-4565-9164-39C4925E467B"), 0, IntPtr.Zero, np)
Dim path As String = Marshal.PtrToStringUni(np)
Marshal.FreeCoTaskMem(np)
Console.WriteLine(path)

Also add to your module or class:

<DllImport("shell32")>
Private Function SHGetKnownFolderPath(ByRef rfid As Guid, ByVal dwFlags As UInt32, ByVal hToken As IntPtr, ByRef np As IntPtr) As Int32 : End Function

Wednesday, October 18, 2017 6:02 PM | 1 vote

Additionally, the program files folder and its sub-folders are typically protected by security descriptors such that write access requires the application to be running under an account with elevated privileges.


Thursday, October 19, 2017 5:23 AM

Hi Snowcone_65, 

Based on your description, I agree with Viorel's opinion. The WinAPI method SHGetKnownFolderPath is the only correct way to retrieve paths to special folders - including the personal ones and the Downloads folder.

 Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim downloadsPath As String = KnownFolders.GetPath(KnownFolder.Downloads)
        Console.WriteLine(Convert.ToString("Downloads folder path: ") & downloadsPath)
        Console.ReadLine()
    End Sub
End Class

''' <summary>
''' Class containing methods to retrieve specific file system paths.
''' </summary>
Public Class KnownFolders
    Private Sub New()
    End Sub
    ' Contacts
    ' Desktop
    ' Documents
    ' Downloads
    ' Favorites
    ' Links
    ' Music
    ' Pictures
    ' SavedGames
    ' SavedSearches
    ' Videos
    Private Shared _knownFolderGuids As String() = New String() {"{56784854-C6CB-462B-8169-88E350ACB882}", "{B4BFCC3A-DB2C-424C-B029-7FE99A87C641}", "{FDD39AD0-238F-46AF-ADB4-6C85480369C7}", "{374DE290-123F-4565-9164-39C4925E467B}", "{1777F761-68AD-4D8A-87BD-30B759FA33DD}", "{BFB9D5E0-C6A9-404C-B2B2-AE6DB6AF4968}",
        "{4BD8D571-6D19-48D3-BE97-422220080E43}", "{33E28130-4E1E-4676-835A-98395C3BC3BB}", "{4C5C32FF-BB9D-43B0-B5B4-2D72E54EAAA4}", "{7D1D3A04-DEBB-4115-95CF-2F29DA2920DA}", "{18989B1D-99B5-455B-841C-AB7C74E4DDFC}"}

    ''' <summary>
    ''' Gets the current path to the specified known folder as currently configured. This does
    ''' not require the folder to be existent.
    ''' </summary>
    ''' <param name="knownFolder">The known folder which current path will be returned.</param>
    ''' <returns>The default path of the known folder.</returns>
    ''' <exception cref="System.Runtime.InteropServices.ExternalException">Thrown if the path
    '''     could not be retrieved.</exception>
    Public Shared Function GetPath(knownFolder As KnownFolder) As String
        Return GetPath(knownFolder, False)
    End Function

    ''' <summary>
    ''' Gets the current path to the specified known folder as currently configured. This does
    ''' not require the folder to be existent.
    ''' </summary>
    ''' <param name="knownFolder">The known folder which current path will be returned.</param>
    ''' <param name="defaultUser">Specifies if the paths of the default user (user profile
    '''     template) will be used. This requires administrative rights.</param>
    ''' <returns>The default path of the known folder.</returns>
    ''' <exception cref="System.Runtime.InteropServices.ExternalException">Thrown if the path
    '''     could not be retrieved.</exception>
    Public Shared Function GetPath(knownFolder As KnownFolder, defaultUser As Boolean) As String
        Return GetPath(knownFolder, KnownFolderFlags.DontVerify, defaultUser)
    End Function

    Private Shared Function GetPath(knownFolder As KnownFolder, flags As KnownFolderFlags, defaultUser As Boolean) As String
        Dim outPath As IntPtr
        Dim result As Integer = SHGetKnownFolderPath(New Guid(_knownFolderGuids(CInt(knownFolder))), CUInt(flags), New IntPtr(If(defaultUser, -1, 0)), outPath)
        If result >= 0 Then
            Return Marshal.PtrToStringUni(outPath)
        Else
            Throw New ExternalException("Unable to retrieve the known folder path. It may not " + "be available on this system.", result)
        End If
    End Function

    <DllImport("Shell32.dll")>
    Private Shared Function SHGetKnownFolderPath(<MarshalAs(UnmanagedType.LPStruct)> rfid As Guid, dwFlags As UInteger, hToken As IntPtr, ByRef ppszPath As IntPtr) As Integer
    End Function

    <Flags>
    Private Enum KnownFolderFlags As UInteger
        SimpleIDList = &H100
        NotParentRelative = &H200
        DefaultPath = &H400
        Init = &H800
        NoAlias = &H1000
        DontUnexpand = &H2000
        DontVerify = &H4000
        Create = &H8000
        NoAppcontainerRedirection = &H10000
        AliasOnly = &H80000000UI
    End Enum
End Class

''' <summary>
''' Standard folders registered with the system. These folders are installed with Windows Vista
''' and later operating systems, and a computer will have only folders appropriate to it
''' installed.
''' </summary>
Public Enum KnownFolder
    Contacts
    Desktop
    Documents
    Downloads
    Favorites
    Links
    Music
    Pictures
    SavedGames
    SavedSearches
    Videos
End Enum

Best Regards,

Cherry

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


Thursday, October 19, 2017 6:45 PM

I dont understand how I would start a program in the downloads folder


Thursday, October 19, 2017 9:28 PM

 You could also try this...

Dim DownloadsFolderPath As String = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Downloads")

 

  Why would you move the file to the Programs folder?  Normally you would use a program installer which would install the file(s) into the programs folder.  You will also have to run your application as an administrator in order to be able to write/copy a file to the Programs folder since it is a protected folder.

Example...

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim TheFilename As String = "Bfxr_WIN.zip"
        Dim SourceFilePath As String = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Downloads", TheFilename)
        Dim DestinationFilePath As String = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), TheFilename)
        Try
            IO.File.Copy(SourceFilePath, DestinationFilePath)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

If you say it can`t be done then i`ll try it


Tuesday, September 10, 2019 2:26 PM

Maybe it is useful for anyone. You can declare it as "Shared" if it throws an error:

<DllImport("shell32")>
Private Shared Function SHGetKnownFolderPath(ByRef rfid As Guid, ByVal dwFlags As UInt32, ByVal hToken As IntPtr, ByRef np As IntPtr) As Int32 : End Function

Regards

Carlos Adrián Martínez