By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,152 Members | 1,647 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,152 IT Pros & Developers. It's quick & easy.

How do you read from a text file on the internet?

P: 6
Reading from a text file on your local computer is easy, but surely using a URL to point to a different location should work too?

The code I have thus far is as follows

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.IO;
  3.  
  4. namespace csharp_station.howto
  5. {
  6.     class TextFileReader
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             // create reader & open file
  11.             TextReader tr = new StreamReader("http://jamesgeddes.com/wttr.txt");
  12.  
  13.             // read a line of text
  14.             Console.WriteLine(tr.ReadLine());
  15.  
  16.             Console.WriteLine("Enter your name");
  17.             string s7 = Console.ReadLine();
  18.             Console.WriteLine("Hello, {0}!", s7);
  19.             // close the stream
  20.             tr.Close();
  21.         }
  22.     }
  23. }
However when this runs the "ArgumentException was unhandled" box pops up and says "URI formats are not supported."

All I want to do is read the contents of that text file, use it as the value of a variable or array and display it on the console. Is this possible?

Thanks everyone!
Feb 12 '10 #1
Share this Question
Share on Google+
2 Replies


tlhintoq
Expert 2.5K+
P: 3,525
I am so far out of my element here it's not funny... but I'm going to assume the streamreader just doesn't understand HTTP communication.

Using the URL you provided a web browser has no issue with it. Had you considered using a webbrowser control? Let it do all the communication/translation then ask it for the content it obtained?
System.Windows.Forms.WebBrowser

I just dragged on onto a test form, gave it your URI and when run I see the "COMPLETE" text that is in your .txt

It looks to have a "DocumentCompleted" event... So I would guess that if you respond to this then you would know when you can get your content.
Feb 12 '10 #2

tlhintoq
Expert 2.5K+
P: 3,525
I am so far out of my element here it's not funny... but I'm going to assume the streamreader just doesn't understand HTTP communication.

Using the URL you provided a web browser has no issue with it. Had you considered using a webbrowser control? Let it do all the communication/translation then ask it for the content it obtained?
System.Windows.Forms.WebBrowser

I just dragged on onto a test form, gave it your URI and when run I see the "COMPLETE" text that is in your .txt

It looks to have a "DocumentCompleted" event... So I would guess that if you respond to this then you would know when you can get your content.
Feb 12 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.