How to read one line of a text file on a web server using a windows application

FartNocker

Regular
Joined
Dec 7, 2003
Messages
97
Location
Western North Carolina
How do I read one line from a text file which is located on a web server using a windows application. I can't seem to find the answers to what I am looking for here. I can display the text file in a browser but this isn't what I want. I just want to read the line and use the data with out using a browser...

Thanks for any Ideas or links,
Mark
 
the simple way?
you may want to use the WebClient class in System.Net to download the file. it's very helpful. from that on its either using the byte[] array or saving to a file on disk and using it later on :)
 
Menge said:
the simple way?
you may want to use the WebClient class in System.Net to download the file. it's very helpful. from that on its either using the byte[] array or saving to a file on disk and using it later on :)

Thanks for the info... I did try this BUT... All I got was some HTML code that is not a part of the simple text file. The small bit of plain text that is in the file did not come in? Just the HTML headers and stuff you would find associated with a .htm web page.
 
Just tried a test using the following code and it read the first line of the text file correctly.
C#:
 System.Net.WebClient wc = new System.Net.WebClient();
 System.IO.Stream s= wc.OpenRead("http://localhost/test.txt");
 System.IO.StreamReader sr = new System.IO.StreamReader(s);
 string st;
 st=sr.ReadLine();

when you say you received a lot of html - what was the content of the HTML (error message / status code)?
 
PlausiblyDamp said:
Just tried a test using the following code and it read the first line of the text file correctly.
C#:
 System.Net.WebClient wc = new System.Net.WebClient();
 System.IO.Stream s= wc.OpenRead("http://localhost/test.txt");
 System.IO.StreamReader sr = new System.IO.StreamReader(s);
 string st;
 st=sr.ReadLine();

when you say you received a lot of html - what was the content of the HTML (error message / status code)?

First off, thanks for responding. I am getting the correct file but it looks as if it is some how being converted to a html file. A file that only contains the numbers "123456789" and ends in a .txt extension. If I use the webclient to save the file using an .htm extension, then I can double click on it and see the correct contains. But if I view the source code of the saved .htm file I don’t see the numbers. When I use the method to read the file as you mentioned above I get the tags that would normally be associated with a html page but the numbers that were in the file are not in it. All I want is the to be able to read the numbers.

The code I used to download was
Visual Basic:
Dim client = New WebClient
client.DownloadFile(UpdateURL, "page.htm")

And the resulting source code for the downloaded file was

Visual Basic:
<HTML><HEAD>
<META NAME="description" CONTENT="MyHomeSite.com">
<META NAME="keywords" CONTENT="">
</HEAD>
<FRAMESET border=0 rows="100%,*" frameborder="no" marginleft=0 margintop=0 marginright=0 marginbottom=0>
<frame src="http://pages.charter.net/MyHomeSite/Numbers.txt" scrolling=auto frameborder="no" border=0 noresize>
<frame topmargin="0" marginwidth=0 scrolling=no marginheight=0 frameborder="no" border=0 noresize>
</FRAMESET>
</HTML>

If I save the file as .txt then it will contain tha exact code above but not the numbers I wanted from the file.

If I use the stream thing I get the same code. NO NUMBERS?

Keep in mind that I made the file with Notepad and uploaded it and it only contains the numbers 123456789

Thank YOU
MARK
 
Last edited:
I just converted your example to VB
Visual Basic:
Dim wc As New System.Net.WebClient
Dim s As System.IO.Stream = wc.OpenRead(UpdateURL)
Dim sr As New System.IO.StreamReader(s)
Dim st As String
st = sr.ReadLine()
msgbox(st)

And I got enpty string.
 
it's probably because your host is embedding all files into a frame for address masking. check if you have that enabled. from the HTML page it seems u accessed the txt file but the host masked it into a framed page.

check that with your web host
 
Last edited:
Menge said:
it's probably because your host is embedding all files into a frame for address masking. check if you have that enabled. from the HTML page it seems u accessed the txt file but the host masked it into a framed page.

check that with your web host

I called them and that said no about the masking. They sais it was just a stright forward setup. I wonder why when I read the file into a stream I only get an empty String? i.e. ""

Mark
 
I figured out the problem guys. I have a Domain Name which is pointed to my free web space on charter. Using the Domain Name it won't work, but, using the direct address to my free web space on charter's servers it works perfect. So in a way you were correct about the masking. It was about to drive me craZY .... LOL There's always a simple solution, finding it is the hard part. Thanks again guys for the input

Mark
 
Back
Top