Alexander Falkesund Alexander Falkesund - 5 months ago 30
Vb.net Question

How do I download a TXT and then read it?

Okay so I have a program that is supposed to download a TXT file which ONLY contains a URL from my Dropbox, save it on my desktop, and then open the webbrowser like

Process.Start(DownloadedFile)
.

My code looks like this:

Dim path As String = "C:\Users\" & SystemInformation.UserName & "\Desktop"
My.Computer.Network.DownloadFile("https://www.dropbox.com/s/uy9jpt1em3o6khp/download_location.txt?dl=1", path + "\" + "Download_Location" + ".txt")

Dim fileReader As String
fileReader = My.Computer.FileSystem.ReadAllText(path + "\Download_Location.txt")
Process.Start(fileReader)


Now my problem is that, the program DOES download my txt file and name it correctly. But even if I know that there is text in the text file, when the program downloads it and saves it on my Desktop named "Download_Location.txt", it is empty. The downloaded file doesn't conatin any text even if Im 100% sure that there should be a URL text.

And when my program reached the code where
Process.Start(fileReader)
it gives me an error:


You can not start the process because no file name is specified.


Thanks a lot! And sorry for my bad english.

Answer

When downloading files via a URL you are required to use a direct URL, due to that you download exactly the URL you specify. The reason you can download it from your web browser is because it first loads the page from the first link, then the page tells the browser to redirect to the actual file.

Direct links through DropBox are accessed via the dl.dropboxusercontent.com website, which is what you'll need to get the direct link to your file. The reason DropBox uses this system is probably because dropbox.com/s/... is yet a bit shorter than dl.dropboxusercontent.com/content_link/...

Having this said, switching your download URL from what you have now to: https://dl.dropboxusercontent.com/content_link/Uxdm1CQxQ50LBm5QejnkGLOIXJf8QL2I‌​ui95XjhMUL2Wz4gjzBwiHttuA07RkbOl/file?dl=1 should work.