Download many small files with FTP quickly - vb.net

I have a working code to download many files (hundreds) from an FTP server, but it's very slow and often a timeout error appears.
This is my current way of doing the downloads:
Using ftpClient As New WebClient()
ftpClient.Credentials = New System.Net.NetworkCredential(ftpuser, ftppassword)
For i As Integer = 0 To directoriesDownload.Count - 1
If directoriesDownload(i).Contains(".") Then
If Sync_BackgroundWorker.CancellationPending = True Then
Exit Sub
End If
Dim path As String = "ftp://" & ftpserver & "Datenbank/" + directoriesDownload(i).ToString()
Dim trnsfrpth As String = config.rootpath & "ServerDownload\" + directoriesDownload(i).ToString()
ftpClient.DownloadFile(path, trnsfrpth)
filenametodownload = directoriesDownload(i).ToString()
filesdownloaded += 1
Sync_BackgroundWorker.ReportProgress(filesdownloaded)
End If
Next
ftpClient.Dispose()
End Using
Is there any faster way of downloading hundreds of small files (up to 10 KB) from an FTP server in VB.NET?
It would be the best if there is an option to sign into the FTP only once instead of logging in and out for every file.
I found somebody else having the same problem but without a working result:
Using FTP to download each file *WHILE* getting the file list
I also tried multithreading with a Parallel.For loop, but WebClient does not work with multithreading. Same thing if I try with ftpClient.DownloadFileAsync(New Uri(path), trnsfrpth).

Is there any faster way of downloading hundreds of small files (up to 10 KB) from an FTP server in VB.NET?
...
I also tried multithreading with a Parallel.For loop, but WebClient does not work with multithreading. Same thing if I try with ftpClient.DownloadFileAsync(New Uri(path), trnsfrpth).
Multithreading is the way to go. It's not true that WebClient does not support multithreading. Why wouldn't it?
If you have a problem with implementing multithreaded FTP transfers, you should ask a question about that, rather than asking a question about other (and probably non-existent) ways.
It would be the best if there is an option to sign into the FTP only once instead of logging in and out for every file.
Your code does sign into FTP only once.
See C# - FtpWebRequest - Multiple requests over the same connection/login - What is written there about FtpWebRequest is equally true for WebClient, as WebClient uses FtpWebRequest internally.

Related

Cloning a csv file to memory and using it in a datatable? (vb.net)

I have a question about the following code. in order to prevent problems caused by file locking I came across the following code.
Dim OrignalBitmap As New Bitmap(Application.StartupPath & "\IMAGES\BACKGROUND_LARGE.jpg")
Dim CloneBitmap As New Bitmap(OrignalBitmap)
OrignalBitmap.Dispose()
Which works like a charm. Now I have all the images in place and I can still access them as a file without anything locking. It works so well for what I need that I was thinking if its possible to do this for file formats other than images such as Csv files which are then used in a datagridview as a bound table?
Usually it is enough to open a File like this, so that it will not block other programs to access and open it.
Dim path1 As String = "C:\temp\temp.csv"
Using fs As FileStream = File.Open(path1, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)
' Do something with filestream
End Using
this will prevent even huge files to open without blocking access
you should check https://learn.microsoft.com/de-de/dotnet/api/system.io.file.open?view=netframework-4.8

Download file from FTP without caching in VB.NET

I know there are several solutions in other languages but I can't find anything in VB.NET.
I have a file containing the "software version" on an FTP server, however everytime the program tries to read it through the WebBrowser, it picks up an old version of it. In other words the cached version.
Is there anything that can use to clear the cache for just the one file?
Code so far:
Dim WebBR As New WebBrowser
WebBR.Navigate("URL\Version.txt", False)
Do Until WebBR.ReadyState = WebBrowserReadyState.Complete
Application.DoEvents()
Loop
Dim VRB = WebBR.Document.Body.InnerHtml
VRB = VRB.Replace("<PRE>", "")
VRB = VRB.Replace("</PRE>", "")
NewVersion = VRB
If NewVersion <> CurrentVersion Then
Return True
Else
Return False
End If
Why do you read the file though web browser? That's an overkill.
Use the FtpWebRequest. It does not have any cache.
See also:
Upload and download a binary file to/from FTP server in C#/.NET
How to: Download Files with FTP.

create txtfile from website and save to server

I have attempted to find a way to download a file (from a PC running a Winform written in VB) from a web directory but could not get this done due to the date stamp that gets generated once the file is saved (filename must be precise). So now I'm trying the reverse (saving the file to the PC directly from the creation of the file).
Does anyone have any suggestions on methods to use (please no FTP due to proxy restrictions, same goes for client/server TCP/UDP). this will always be a .txt file.
thanks in advance.
If you can see the file contents by navigating to the URL, you can use an HttpWebRequest and a StreamReader / StreamWriter to save the data.
I'm downloading an example from http://textfiles.com/100/914bbs.txt
Dim request As HttpWebRequest
request = WebRequest.Create("http://textfiles.com/100/914bbs.txt")
request.Method = "GET"
Dim response = request.GetResponse
Using reader As New StreamReader(response.GetResponseStream)
Using writer As New StreamWriter("C:\myfilename.txt")
writer.Write(reader.ReadToEnd)
End Using
End Using

How to improve my auto-update functionn? VB.net

I'm still learning VB.net and I'm now at a stage where I want to write an auto-update function, now I wrote this simple function myself, nothing fancy but I'd just like to check to see that there's no major flaws in my logic? Short of human error I think this is a nice simple way to do this.
Note: it all works flawlessly from my testing.
My Function
Public Function updateCheck()
Dim CurrentVersion As String = My.Settings.currentVersion
Dim updateURL As String = My.Settings.updateURL
Dim WebRequest As WebClient = New WebClient
Dim Version As String = WebRequest.DownloadString(updateURL)
If Version = CurrentVersion Then
MessageBox.Show("no updates available")
Else
MessageBox.Show("An new version is available: " & Version)
End If
End Function
updatecheck.html file simply contains "vx.x.x" which sites on a web-server and the currentVersion string is again "vx.x.x"
I can't see this failing short of forgetting to change the currentVersion string upon an application update and it looping.
In terms of simplistic and clean code, is there anyway I can improve this? - I plan on adding some download and execute code to download an updater which un-installs and re-installs the latest version. - I'm currently using InstallShield to deploy the application.
Thanks for any suggestions/comments.
Instead of trying to code this yourself - you should have a look at ClickOnce deployment.
This has all the functionality you are trying to code and handles all the error cases when there is no connection, etc. It also allows for install without admin rights.

Download file in VB.NET 2010

I have looked almost everywhere on the internet and I cannot find a way to download a file from the internet into a specific folder that works with VB.NET 2010. I would like to download a file called, for instance, example.txt, and download it into, for example, %HOMEDRIVE%%HOMEPATH%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup so that it will run automatically at system startup. All help is appreciated
Guessing something based on...
Using webClient = New WebClient()
Dim bytes = webClient.DownloadData("http://www.google.com")
File.WriteAllBytes(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MyFileName.ext"), bytes)
End Using
As for the startup, VB.NET has a pretty ease way to add Registry keys...
My.Computer.Registry.SetValue
To set something like HKEY_CURRENT_USER\Software\Microsoft\CurrentVersion\Run
UPDATE
How to: Create a Registry Key and Set Its Values in Visual Basic
http://msdn.microsoft.com/en-us/library/cy6azwf7(v=VS.100).aspx
I would suggest using WebClient.DownloadFile. Use Environment.SpecialFolder.Startup to get the path to save the file.
Sub Main()
Using wc As New WebClient()
Dim startupPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup)
wc.DownloadFile("http://MyDomain.com/MyFile.txt", Path.Combine(startupPath, "test.txt"))
End Using
End Sub