# Reading text files on the internet

This is a discussion on Reading text files on the internet within the C# Programming forums, part of the General Programming Boards category; Can anyone tell me how to read a text file or a picture on the internet? For example, I want ...

1. ## Reading text files on the internet

Can anyone tell me how to read a text file or a picture on the internet? For example, I want to compare the data in a string called "data", with a text file, for example, located at http://www.textfiles.com/text.txt. How would I go about downloading the text file into a string? I can do this locally with a file on my computer using:
Code:
string user="C:\stuff\text.txt";
string tmp="";
{
byte[] b = new byte[1024];
UTF8Encoding temp = new UTF8Encoding(true);

while (fs.Read(b, 0, b.Length) > 0)
{
tmp += (temp.GetString(b));
}
}
Like I said, this works for local files, so how can I modify this to read the file online instead?
Thanks

2. I just read on another forum that I would have to download the file first and then access it locally like I do now. Does anyone know how I can download the file?

First item: http://www.primaryobjects.com/CMS/Article64.aspx

4. Well, I figured out how to do it. I used
Code:
string url="http://www.textfiles.com/text.txt";
WebRequest request = WebRequest.Create(url);
request.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream dataStream = response.GetResponseStream();
dataStream.Close();
response.Close();
It works, but is a bit slow. My second question is: How do change the code to write the data to the file instead of reading it?

5. did you try to read it in and store it, then alter it, then use StreamWriter to write it back?

6. I have now, but I don't know how to do that. Could you perhaps give me an example of how to do it?

7. well chances are you will have to upload it via some http since most web sites do not have direct write access to the files you can view.