FTP Download to specified Directory Error
HI,
Desktop app VS 2022 VB.net VB
I'm trying to FTP a file to "My Documents/PDFs" folder with a desktop app. I have the FTP working just fine but when I set/change the destination path to "My Documents" , "Documents" or "Downloads" (preferred) I get and error
" InnerException: {"Could not find a part of the path 'C:\Downloads\61503.pdf'."}"
I know the C:\downloads directory exists.
Any Ideas?
Code:
Dim FilenameIs As String = ("61503.pdf")
Dim destinationPath As String = ("C:/Downloads\61503.pdf")
Dim client As New WebClient()
client.Credentials = New NetworkCredential("SigPics", "MtPWD") ' Replace with actual credentials
Try
'client.DownloadFile("ftp://ftp.dashleywebs.com/61503.jpg", "C:\Users\Public\Pictures\" & FilenameIs & "") ' Replace with actual paths
client.DownloadFile("ftp://ftp.dashleywebs.com/61503.jpg", destinationPath) ' Replace with actual paths
Console.WriteLine("File downloaded successfully using WebClient.")
Catch ex As Exception
Console.WriteLine("Error downloading file: " & ex.Message)
End Try
Re: FTP Download to specified Directory Error
Code:
C:/Downloads\61503.pdf
Should be:
Code:
C:\Downloads\61503.pdf
Re: FTP Download to specified Directory Error
@jdc2000
I had it that way and and tried both slashes. Either way with C:\Downloads\61503.pdf I still get
InnerException: {"Could not find a part of the path 'C:\Downloads\61503.pdf'."}
If I change the destination to Dim destinationPath As String = ("C:\Users\Public\Pictures\61503.jpg")
It downloads to that folder. Just can't get it into Downloads folder
Re: FTP Download to specified Directory Error
What version of .NET are you using? Also, what is the Exception (not just the InnerException)?
The reason I ask is because in .NET core, Microsoft's official position is (reference):
Quote:
Recommended action
Use the System.Net.Http.HttpClient class instead.
For FTP, since HttpClient doesn't support it, we recommend using a third-party library.
And for the Exception, it very well could be a permissions issue. You can double-check this by running your application as an administrator.
Re: FTP Download to specified Directory Error
For FTP just try FluentFTP NuGet package. It is open source, MIT license.
Source code link: https://github.com/robinrodricks/FluentFTP
Check this sample (from the repository in GitHub):
https://github.com/robinrodricks/Flu...ownloadFile.vb
Re: FTP Download to specified Directory Error
Does C:\Downloads exists??
If you refer to the Download folder this is located in your user folder.
Something like this:
C:\Users\Arnoutdv\Downloads
Re: FTP Download to specified Directory Error
The cure was
Dim userprof As String = Environ("USERPROFILE")
Dim destinationPath As String = (userprof & "\downlaods\61503.jpg")
It had to go to c:\users\<username>\downloads\myfilename
or in m y case c:\users\dan\downloads\filename
Thanks Arnoutdv
-dan
Re: FTP Download to specified Directory Error
Ah, I incorrectly assumed that you did indeed want to go to C:\downloads and not the user profile's downloads. In that case, use this instead, it is a much better solution:
Code:
Private Function GetDestinationPath(filename As String) As String
Dim userProfilePath As String = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)
Dim destinationPath As String = IO.Path.Combine(userProfilePath, "Downloads", filename)
Return destinationPath
End Function
' usage: Dim destinationPath As String = GetDestinationPath("61503.jpg")
Re: FTP Download to specified Directory Error
@dday C:\downloads was my intent but the user profile' downloads works just as well. I Like the function you provided to so I shall use it. :) Since this app wil run on multiple desktops the use profile is still good.
to answer your ealier question(s) The framework is 4.7.2 and the actual error was: Message: "An exception occurred during a WebClient request." Status: UnknownError {16}.
I agree I think it was a permissons issue. Dosen't matter now. :) It's working with the user profile downloads so I'm content.