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

Posted on 2005-03-15
Medium Priority
Last Modified: 2010-04-16

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.

Question by:likhith123
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 14

Accepted Solution

Thandava Vallepalli earned 2000 total points
ID: 13544662
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
      // 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);



Author Comment

ID: 13550257
Hi Krishna,

Thank you.


Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question