Click here to Skip to main content
15,867,594 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi there. I'm wondering how I would be able to get the path for the user's download folder (in Windows Vista and 7) in VB.NET.

My current attempt at accessing this folder was to get the user's music directory, "subtract" the "Music" string from that path, and add "Downloads" to it. However, I got an error that said that it couldn't convert "C:\Users\USERNAME\Music" to a double.

Also, I tried creating the "C:\Users\" path, adding the user's name, and then adding the downloads directory to the end of that. That didn't work out so well, either.

Thanks!
Posted
Updated 4-Feb-20 9:12am

Most special folders can be reached with e.g. Environment.GetFolderPath(Environment.SpecialFolder.MyMusic).
But "Downloads" is not covered by this enumeration...
A solution was shown on stack overflow: http://stackoverflow.com/questions/3795023/downloads-folder-not-special-enough[^].
 
Share this answer
 
Comments
JF2015 29-May-12 3:13am    
Nice one. 5+
The VB.Net (not C#) function that I use is following

<DllImport("shell32.dll")>
Private Function SHGetKnownFolderPath _
    (<MarshalAs(UnmanagedType.LPStruct)> ByVal rfid As Guid _
    , ByVal dwFlags As UInt32 _
    , ByVal hToken As IntPtr _
    , ByRef pszPath As IntPtr
    ) As Int32
End Function
    
Public Function GetDownloadsFolder() As String
    Dim sResult As String = ""
    Dim ppszPath As IntPtr
    Dim gGuid As Guid = New Guid("{374DE290-123F-4565-9164-39C4925E467B}")
    
    If SHGetKnownFolderPath(gGuid, 0, 0, ppszPath) = 0 Then
        sResult = Marshal.PtrToStringUni(ppszPath)
        Marshal.FreeCoTaskMem(ppszPath)
    End If
    Return sResult
End Function


In my program, I call it to move some CSV files in another folder using following code.

Dim sDownloadFolder = GetDownloadsFolder()
Dim di = New DirectoryInfo(sDownloadFolder)

'Move all CSV files that begin with BE in specific folder
'that has been defined in a CONFIG file (variable: sExtractPath

For Each fi As FileInfo In di.GetFiles("BE*.csv")
    Dim sFilename = sExtractPath & "\" & fi.Name
    File.Move(fi.FullName, sFilename)
Next
 
Share this answer
 
The method I used us to call the special folders and build it myself as follows: Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Downloads\"
 
Share this answer
 
Comments
OriginalGriff 26-Apr-21 11:03am    
While I applaud your urge to help people, it's a good idea to stick to new questions, rather than 8 year old ones. After that amount of time, it's unlikely that the original poster is at all interested in the problem any more!
Answering old questions can be seen as rep-point hunting, which is a form of site abuse. The more trigger happy amongst us will start the process of banning you from the site if you aren't careful. Stick to new questions and you'll be fine.
Richard Deeming 27-Apr-21 4:24am    
Aside from the age of the question, your solution will produce the wrong path if the downloads folder has been moved outside of the user's profile.

This is easily done via the "Location" property page on the properties of the folder, or via Group Policy.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900