VB.NET - Checking and Executing Updates? - vb.net

So I have a very small bit of VB.NET that can check fine if theres an Update.
But theres a few issues:
It does this all while not stopping the main code from executing;
It wont even download the file;
I cant get it to close the app that found the Update/Downloaded the Update and then Open the new file.
How can I get it to do all of that?
Current Code:
Public Sub CheckForUpdates()
'Connect to the Version File and Open It/Read It's Contents;
Dim request As HttpWebRequest = WebRequest.Create("https://dl.dropboxusercontent.com/s/rrk7yhfjvy500jl/version.txt")
Dim response As HttpWebResponse = request.GetResponse()
Dim sr As StreamReader = New StreamReader(response.GetResponseStream())
'Set the Variables to the Appropriate Versions;
Dim newestversion As String = sr.ReadToEnd()
Dim currentversion As String = Application.ProductVersion
'If the program isn't up to date;
If Not newestversion.Contains(currentversion) Then
MessageBox.Show("Downloading update!")
'Download the Update;
Using wc = New WebClient()
'Download the newest EXE file and store it in the same Directory as the Current EXE file;
wc.DownloadFile("https://dl.dropboxusercontent.com/s/da3in67jjayvqsz/PRAGMA1.exe", Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Startup), "PRAGMA1.exe"))
End Using
End If
End Sub
(It doesnt work)

Related

VB.NET to download latest file name with timestamp in its name from FTP server

I want to pull a file from my FTP server into my local drive. However the most difficult part of do this is that the file name is a timestamp that changes on a daily basis. The program should download the file regardless of how it has changed with the date; year, month, hour, minutes, and seconds. The format of the name are always the same. File name example is below in bold. Please advise!
For example,
username - meUser
password - mepswrd
and the URL to the FTP
/afea/euser/aefe/aole/efa/
and the path I want the file to save after download it.
C:\Users\alae\Desktop\loaef
and the file is in FORMAT
20160223.171234.BA_DESRP_20160121.txt
The only part to the file that doesn't change is BA_DESRP, all the other part can change as it is a timestamp.
This is my code to start with:
Const lf As String = "C:\Users\alae\Desktop\loaef"
Const rf As String = "/afea/euser/aefe/aole/efa/"
Const ht As String = "host"
Const un As String = "username"
Const pw As String = "password"
Dim URI As String = ht & rf
Dim ftp As System.Net.FtpWebRequest = _
CType(FtpWebRequest.Create(URI), FtpWebRequest)
ftp.Credentials = New _
System.Net.NetworkCredential(un, pw)
There's no easy way with the FtpWebRequest (or any other functionality readily available in the .NET framework). You have to:
List the remote directory using the WebRequestMethods.Ftp.ListDirectory
Filter the returned list to those containing the BA_DESRP
Select the latest out of those
And download it
Dim url As String = "ftp://ftp.example.com/remote/path/"
Dim credentials As NetworkCredential = New NetworkCredential("username", "password")
Const localPath = "C:\local\path"
Dim listRequest As FtpWebRequest = WebRequest.Create(url)
listRequest.Method = WebRequestMethods.Ftp.ListDirectory
listRequest.Credentials = credentials
Dim latest As String = Nothing
Using listResponse As FtpWebResponse = listRequest.GetResponse(),
listStream As Stream = listResponse.GetResponseStream(),
listReader As StreamReader = New StreamReader(listStream)
While Not listReader.EndOfStream
Dim filename As String = listReader.ReadLine()
If filename.Contains("BA_DESRP") Then
Console.WriteLine("Found {0} ...", filename)
If (latest Is Nothing) OrElse (latest < filename) Then
latest = filename
End If
End If
End While
End Using
If Not latest Is Nothing Then
Console.WriteLine("Downloading {0} ...", latest)
Dim webClient As New WebClient()
webClient.Credentials = credentials
webClient.DownloadFile(url + latest, Path.Combine(localPath, latest))
End If
Or use another FTP library with more powerful functionality.
For example with WinSCP .NET assembly:
Const localPath = "C:\local\path\"
Const remotePath = "/remote/path"
Dim sessionOptions As New SessionOptions
With sessionOptions
.Protocol = Protocol.Ftp
.HostName = "ftp.example"
.UserName = "username"
.Password = "password"
End With
Using session As New Session
session.Open(sessionOptions)
Dim latest As RemoteFileInfo =
session.ListDirectory(remotePath).Files.
Where(Function(file) file.Name.Contains("BA_DESRP")).
OrderByDescending(Function(file) file.Name).
FirstOrDefault()
If Not latest Is Nothing Then
Console.WriteLine("Downloading {0} ...", latest)
session.GetFiles(latest.FullName, localPath).Check()
End If
End Using
If you can use the actual file timestamp (not the timestamp in its name), it would be even more straightforward. See Downloading the most recent file.
(I'm the author of WinSCP)
This will get you the list of files in the directory. Since your file name already includes the timestamp, you just need to parse it out from the file name, and then ultimately store each timestamp in a variable where you can then compare the next file's time stamp to. if next file is greater than the value you in your variable, assign that value. finally when you are done, your var should reflect the file name you need and at that stage you can call the download method to get the file.
Dim request As FtpWebRequest = WebRequest.Create("ftp://" & "servername" & "/" & "directory" & "/*")
request.Method = WebRequestMethods.Ftp.ListDirectory
request.Credentials = New NetworkCredential("username", "password")
Using reader As New StreamReader(request.GetResponse().GetResponseStream())
Do Until reader.EndOfStream
Console.WriteLine(reader.ReadLine())
Loop
End Using
since you say thousands for files, this route may not be the best option for you. if you are not restricted to .NET, you may want to explore alternative options, such as using a ftp client where you may be able to search by the actual time stamp on files...

if else for run file and stream file generate vb.net

How to First Run File seconds stream file in text box via if and else
If Process.Start("test.bat") Then
Dim address As String = "id.txt"
Dim client As WebClient = New WebClient()
Dim reader As StreamReader = New StreamReader(client.OpenRead(address))
TextBox1.Text = reader.ReadToEnd
End If
Note: text file generated via batch file.
Try
Dim proc = Process.Start("test.bat")
proc.WaitForExit()
Using client As New WebClient()
TextBox1.Text = client.DownloadString(File.ReadAllText("id.txt"))
End Using
Catch
End Try

vb.net downloading file using WebClient always corrupted file

I'm trying to download a ZIP file from Dropbox but every time I try it stops and only downloads ~100 kb of the file. It seems to corrupt any file I download from it, but however if I try to download using a normal browser it works.
Dim remoteUri As String = "https://www.dropbox.com/-/-------/test.zip?dl=0"
Dim fileName As String = "test.zip"
Dim myStringWebResource As String = Nothing
Dim myWebClient As New WebClient()
myStringWebResource = remoteUri + fileName
myWebClient.DownloadFile(myStringWebResource, fileName)
Your remoteUri is wrong. You're adding the filename twice. That gives you a bad url.
Dim remoteUri As String = "https://www.dropbox.com/-/-------/{0}?dl=1"
Dim fileName As String = "test.zip"
Dim myStringWebResource As String = Nothing
Dim myWebClient As New WebClient()
myStringWebResource = String.Format(remoteUri, fileName)
myWebClient.DownloadFile(myStringWebResource, fileName)
Try adding dl=1 to force download.

Writing FileStream to Local Path and not the server

I have the following code working successfully:
Protected Sub ExportExcel_Click(sender As Object, e As EventArgs) Handles ExportExcel.Click
Dim warnings As Warning()
Dim streamids As String()
Dim mimeType As String
Dim encoding As String
Dim filenameExtension As String
Dim fileName As String = "D:\Report" & DateTime.Now.ToString("yyyyMMdd_HHmmss") & ".xls"
Dim bytes As Byte() = ReportViewer1.LocalReport.Render("Excel", Nothing, mimeType, encoding, filenameExtension, streamids, warnings)
Using fs As New FileStream(fileName, FileMode.Create)
fs.Write(bytes, 0, bytes.Length)
End Using
lblMessage.Text = Functions.GetMessageConfirm("Report downloaded successfully in your D:/ at: " & Now.ToString)
End Sub
This code saves the file in the web server. I want to save the file on the client machine.
You're halfway there, probably. You can't just save files on the client anyway. Clients are webbrowsers, and they run JavaScript.
What you can do is use the download functionality of webbrowsers, to let them download the file which you just created. To do so, put the output on the server in a directory from where it can be downloaded, then return the new URL to the client.

How do I read text directly from a URL using VB.net?

I'm making a Windows Phone 8 app. I have the latitude and longitude of the target location. I need to get the Two Letter ISO country code of target location.
I'm using the following code to make it happen.
'Dim address As String = puri
'Dim client As WebClient = New WebClient()
'Dim reader As StreamReader = New StreamReader(address)
'code = reader.ReadToEnd
Dim inStream As StreamReader
Dim wr As WebRequest
Dim webresponse As WebResponse
wr = WebRequest.Create(puri)
webresponse = wr.GetResponse()
inStream = New StreamReader(webresponse.GetResponseStream())
code = inStream.ReadToEnd()
where puri(in the commented code) is the address of the webservice in string format.
When trying the commented code, the error I'm getting is that string cannot be converted to system.uri format. (address)
When trying the uncommented code, I get an error which says, getresponse is not a member of class system.net.webrequest()
I guess with the updates to .NET the code changed, but I couldn't find anything current on the topic.
URI = http://api.geonames.org/countryCode?lat=17.60890&lng=76.98966&username=demo
I think you should use WebClient Class instead of a WebRequest. It is simpler and faster. Here is a simple example:
Dim WebCL As New WebClient
Dim DownLoadedText As String = String.Empty
Try
DownLoadedText = WebCL.DownloadString("Your Url")
' Do something
Catch ex As Exception
Throw New Exception("Oops!! ERROR has occured, something is wrong with your address")
End Try