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

Hi,

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');            
      streamWriter.WriteLine(subString[2]+"\t"+subString[3]+"\t"+subString[4]);
      sourceStringLine = streamReader.ReadLine();
}

streamWriter.Close();
streamReader.Close();

------------------------------------------------
 

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

http://sfx.scholarsportal.info/export_files/holdings_uoit.txt

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.

Regards,
Li
likhith123Asked:
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("http://sfx.scholarsportal.info/export_files/holdings_uoit.txt");

            // 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
            ReadWriteStream(resStream,writeStream);
      }
      
      // 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 )
          {
              writeStream.Write(buffer,0,bytesRead);
              bytesRead = readStream.Read(buffer,0,Length);
          }
          readStream.Close();
          writeStream.Close();
      }
      
      
      
}
==================================


http://www.csharp-station.com/HowTo/HttpWebFetch.aspx
http://www.developerfusion.com/show/4669/


itsvtk
0
 
likhith123Author Commented:
Hi Krishna,

Thank you.

Regards,
Likhith
0
All Courses

From novice to tech pro — start learning today.