One important thing to understand is users can and do redirect their downloads folder to weird locations. It's not a good idea at all to assume C:\Users\USERNAME\Downloads
is correct. On my own system, for instance, I have a smallish SSD and a much larger traditional HDD. I prefer my downloads to end up on the HDD, well away from the rest of my profile.
Most of these folders can be had easily using Environment.GetFolderPath()
method. For example, the documents folder is here:
Dim documents As String = Environment.GetFolderPath(SpecialFolder.MyDocuments)
Unfortunately, there's no SpecialFolder
enum value for the Downloads
folder. We have to work a little harder. I prefer this code-project solution:
<DllImport("shell32.dll")>
Private Shared 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
It's really not that much code, and it does things the right way.
Paste the above as new methods in your existing class, add an Imports System.Runtime.InteropServices
to the top of the file, and you can update the code in the question like this:
Private Sub btnCreateFolders_Click(sender As Object, e As EventArgs) Handles btnCreateFolders.Click
Dim SubTest As String = Path.Combine(GetDownloadsFolder(), "TEST\SUB TEST")
Directory.CreateDirectory(SubTest)
End Sub
That said, I also agree with the comment saying Downloads is an unusual place for this. The documents
path used above is much more common.