Jeff Roberts [mailto:[EMAIL PROTECTED]] wrote:

> Does anyone know if I can read a text file from a web server
> with the = FileStream class ?  For example, if my .net
> application was launched from = www.ws.com\myapp.exe, could I
> read a file from www.ws.com by the name of = mytextfile with
> the following code ?

No you can't read it using a file stream. You need to read it by making a
request to the http server and using a NetworkStream. Something like:

<codeSnippet language="C#">
// Build an http request for the file
HttpWebRequest getFileRequest =
(HttpWebRequest)WebRequest.Create("http://somesite.com/someFile.txt";);
getFileRequest.Method = "GET";

// Get the response
HttpWebResponse getFileReponse = getFileRequest.GetResponse();

// Get the response stream and wrap it in a stream reader
// (note: we're allowing the StreamReader to determine encoding)
StreamReader responseReader = new
StreamReader(getFileReponse.GetResponseStream());

// Read the file contents into a string
string fileContents = responseReader.ReadToEnd();

// Do something with the file contents
Debug.WriteLine(fileContents);
</codeSnippet>

HTH,
Drew

[ .NET MVP | weblog: http://radio.weblogs.com/0104813/ ]

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to