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

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 I 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?

Recommended Answers

All 11 Replies

I was able to read "COMPLETE" through the following:

string fileline;
     HttpWebRequest reqFP = 
      (HttpWebRequest) HttpWebRequest.Create("http://jamesgeddes.com/wttr.txt");
     HttpWebResponse rspFP =  (HttpWebResponse)reqFP.GetResponse();
     Stream st = rspFP.GetResponseStream();
     using (StreamReader sr = new StreamReader(st))
     {
           fileline = sr.ReadLine();
     }

How do you use "HttpWebRequest"?

I was able to read "COMPLETE" through the following:

string fileline;
     HttpWebRequest reqFP = 
      (HttpWebRequest) HttpWebRequest.Create("http://jamesgeddes.com/wttr.txt");
     HttpWebResponse rspFP =  (HttpWebResponse)reqFP.GetResponse();
     Stream st = rspFP.GetResponseStream();
     using (StreamReader sr = new StreamReader(st))
     {
           fileline = sr.ReadLine();
     }

It's in the System.Net namespace. Apologies for the omission.

As a little "getting to know your IDE" tip, you can type in the classes you see mentioned into your code and find the right namespace if it has not already been referenced. Sometimes this can lead you astray, as class names are ambiguous across namespaces from time to time, but it is still a helper more often than not. Type in the class name and you will see sort of a compacted box under the last letter. You can hover over that box and then you'll see the option to either add a using statement to the namespace that contains that class, or you can select to add a fully-qualified reference to the class instead.

See the attached image for an example.

can this be used on windows mobile, or would it require more code to interface with the mobile internet connection?

You'd have to poke around at http://msdn.microsoft.com/en-us/netframework/aa497273.aspx (.NET Compact Framework) to see if this portion of the library is incorporated. I don't know much about that end of things or what phones run which libraries.

>can this be used on windows mobile

Yes.

>can this be used on windows mobile

Yes.

I've been testing it, and it won't work without the internet on my laptop! What can be done?

it won't work without the internet on my laptop!

I'm not sure I follow... how would it work without the internet?

I'm not sure I follow... how would it work without the internet?

Windows Mobile on the HD2 can pick up internet when the program is running through "debugging" via active sync, however when I compile it in to a .cab file, install that directly on the HD2 then disconnect the laptop, it crashes because it can't find the internet.

What do I have to do to get it to realise that it can use the mobile internet on the phone?

I gotcha now. That's all unfamiliar to me, unfortunately. Someone else will certainly know more about this area. I would only suggest tagging the thread with WindowsMobile and the name of your phone to hedge your bets.

EDIT: I hadn't stopped into the C# forum directly so I had missed your new post. See what happens with that one.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.