How to open and read the file on the internet (NOT on the local harddrive or on the network drive) ?


This is what I am doing now:

I have the file on the local harddrive. I open it for reading using the following lines of code and then write the required information into another file.
Note:The source file path and filename is taken from the TextBox with the name 'textBoxSourceFile'
        The target file path and filename is taken from the TextBox with the name 'textBoxTargetFile'
StreamWriter streamWriter = new StreamWriter(textBoxTargetFile.Text, false);
FileStream fileStream = new FileStream(textBoxSourceFile.Text, FileMode.Open);
StreamReader streamReader = new StreamReader(fileStream);
sourceStringLine = streamReader.ReadLine();

while(sourceStringLine != null)
      string[] subString ;
      subString = sourceStringLine.Split('\t');            
      sourceStringLine = streamReader.ReadLine();



This is what I would like to do:
Say the text file is on the internet and the URL is (for example):

How will I open a file for reading from the above location?
What should the modified code be to get the URL stuff working?

Thank you.

Who is Participating?
Thandava VallepalliConnect With a Mentor Commented:
Here is ur complete code...... for reading Internet files and saving in local drive.....

using System;
using System.IO;
using System.Net;
using System.Text;

/// <summary>
/// Fetches a Web Page
/// </summary>
class WebFetch
      static void Main(string[] args)
            // used to build entire input
            StringBuilder sb  = new StringBuilder();

            // used on each read operation
            byte[]        buf = new byte[8192];

            // prepare the web page we will be asking for
            HttpWebRequest  request  = (HttpWebRequest)       WebRequest.Create("");

            // execute the request
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            // we will read data via the response stream
            Stream resStream = response.GetResponseStream();
            string saveTo = "c:\\temp.txt";   //  Pass here your physical file path
            // create a write stream
            FileStream writeStream = new FileStream(saveTo, FileMode.Create, FileAccess.Write);
            // write to the stream
      // readStream is the stream you need to read
      // writeStream is the stream you want to write to
      private static void ReadWriteStream(Stream readStream, Stream writeStream)
          int Length = 256;
          Byte [] buffer = new Byte[Length];
          int bytesRead = readStream.Read(buffer,0,Length);
          // write the required bytes
          while( bytesRead > 0 )
              bytesRead = readStream.Read(buffer,0,Length);

likhith123Author Commented:
Hi Krishna,

Thank you.

All Courses

From novice to tech pro — start learning today.