I'm having trouble downloading a text file in Visual Studio Community 2015. It's a text file in my OneDrive Public folder containing the version number of my application (1.0.0.0). The download link I use works perfectly when opened manually, but when my VB code executes, it does download the text file correctly but the file is blank when I open it and I cannot figure out where it's going wrong.
In Module1, I have a sub for downloading and a sub for reading the file afterwards:
Module Module1
' Public variables
Public tempPath As String = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) & "\Temp"
Sub DownloadToTemp(filePath As String)
' Download a text file, ready to read contents
If Not IO.Directory.Exists(tempPath & "\Temp") Then
IO.Directory.CreateDirectory(tempPath & "\Temp")
End If
Try
My.Computer.Network.DownloadFile _
(address:=filePath,
destinationFileName:=tempPath & "\TempText.txt",
userName:=String.Empty,
password:=String.Empty,
showUI:=False,
connectionTimeout:=10000,
overwrite:=True)
Catch ex As Exception
MsgBox("Can't read file" & vbCrLf & ex.Message)
End Try
End Sub
Sub ReadFile()
Dim fStream As New IO.FileStream(tempPath & "\TempText.txt", IO.FileMode.Open)
Dim sReader As New System.IO.StreamReader(fStream)
Dim sArray As String() = Nothing
Dim index As Integer = 0
Do While sReader.Peek >= 0
ReDim Preserve sArray(index)
sArray(index) = sReader.ReadLine
index += 1
Loop
fStream.Close()
sReader.Close()
' Test
Dim fileContents As String = Nothing
If sArray Is Nothing Then
MsgBox("No Data Found")
Exit Sub
End If
For index = 0 To UBound(sArray)
fileContents = fileContents & sArray(index) & vbCrLf
Next
MessageBox.Show(fileContents)
End Sub
End Module
And they're called from the main code:
Private Sub frmSplash_Load(sender As Object, e As EventArgs) Handles MyBase.Load
lblVersion.Text = "v" & Application.ProductVersion
' Check available version online
Call DownloadToTemp("https://onedrive.live.com/download?resid=DE2331D1649390C1!16974&authkey=!AH2cr1S1SHs9Epk&ithint=file%2ctxt")
Call ReadFile()
End Sub
So, everything seems to be correct and working, no errors or exceptions, but the file that my VB code downloads is blank, but manually clicking the download link from the code downloads it with the contents intact. Can anybody see why this would be happening?