Sunday, November 21, 2010

In vb how can I set a var from a text file on the internet?

In visual basic how can I set a variable from a text file that is on my website. EG:



My website is example.com



I have a text file at http://example.com/textfile.txt



I need to know how to make vb go to that url, get the contents of the text file (it will just be a 3 digit number less than 255) then store it in a variable.In vb how can I set a var from a text file on the internet?
You would need to use the System.Net.HttpWebRequest namespace to get this done. Example below:



'Add these lines to the top of your code page

Imports System.Net

Imports System.IO



Function GetFileContents() as String

Dim results as string

Dim url as string = ';http://example.com/textfile.text';

Dim webresponse as HttpWebResponse = HttpWebRequest.Create(url).GetResponse()

Dim reader as streamreader = New Streamreader(webresponse.GetResponseStre鈥?br>
Dim str as string = reader.readline()

Do while not str is nothing

results = str

str = reader.readline()

Loop

return results

end function



You can either return the number from the text file as string (as indicated above) and convert it to an integer if needed back in the main part of your code. You could also change this function to return an integer and convert the results variable to an Integer before returning it.



Hope this helps.



If you need further clarification, you can e-mail me.

No comments:

Post a Comment