Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 120371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:48:03+00:00 2026-05-11T03:48:03+00:00

I am using C# and a console app and I am using this script

  • 0

I am using C# and a console app and I am using this script to download files from a remote server. There area a couple of things I want to add. First, when it writes to a file, it doesn’t take into consideration a newline. This seems to run a certain amount of bytes and then goes to a newline. I would like it to keep the same format as the file it is reading from. Second, there are multiple .jpg files on the server that I need to download. How can I use this script to download multiple, .jpg files

public static int DownLoadFiles(String remoteUrl, String localFile)     {         int bytesProcessed = 0;          // Assign values to these objects here so that they can         // be referenced in the finally block         StreamReader remoteStream = null;         StreamWriter localStream = null;         WebResponse response = null;          // Use a try/catch/finally block as both the WebRequest and Stream         // classes throw exceptions upon error         try         {             // Create a request for the specified remote file name             WebRequest request = WebRequest.Create(remoteUrl);             request.PreAuthenticate = true;             NetworkCredential credentials = new NetworkCredential('id', 'pass');             request.Credentials = credentials;              if (request != null)             {                 // Send the request to the server and retrieve the                 // WebResponse object                 response = request.GetResponse();                 if (response != null)                 {                     // Once the WebResponse object has been retrieved,                     // get the stream object associated with the response's data                     remoteStream = new StreamReader(response.GetResponseStream());                      // Create the local file                     localStream = new StreamWriter(File.Create(localFile));                      // Allocate a 1k buffer                     char[] buffer = new char[1024];                     int bytesRead;                      // Simple do/while loop to read from stream until                     // no bytes are returned                     do                     {                         // Read data (up to 1k) from the stream                         bytesRead = remoteStream.Read(buffer, 0, buffer.Length);                          // Write the data to the local file                         localStream.WriteLine(buffer, 0, bytesRead);                          // Increment total bytes processed                         bytesProcessed += bytesRead;                     } while (bytesRead > 0);                 }             }         }         catch (Exception e)         {             Console.WriteLine(e.Message);         }         finally         {             // Close the response and streams objects here             // to make sure they're closed even if an exception             // is thrown at some point             if (response != null) response.Close();             if (remoteStream != null) remoteStream.Close();             if (localStream != null) localStream.Close();         }          // Return total bytes processed to caller.         return bytesProcessed; 
  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T03:48:03+00:00Added an answer on May 11, 2026 at 3:48 am

    Why don’t you use WebClient.DownloadData or WebClient.DownloadFile instead?

    WebClient client = new WebClient(); client.Credentials = new NetworkCredentials('id', 'pass'); client.DownloadFile(remoteUrl, localFile); 

    By the way the correct way to copy a stream to another is not what you did. You shouldn’t read into char[] at all, as you might run into encoding and end of line issues as you are downloading a binary file. The WriteLine method call is problematic too. The right way to copy contents of a stream to another is:

    void CopyStream(Stream destination, Stream source) {    int count;    byte[] buffer = new byte[BUFFER_SIZE];    while( (count = source.Read(buffer, 0, buffer.Length)) > 0)        destination.Write(buffer, 0, count); } 

    The WebClient class is much easier to use and I suggest using that instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 118k
  • Answers 118k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Look at Assembly Binding Redirection... You can redirect the old… May 11, 2026 at 11:32 pm
  • Editorial Team
    Editorial Team added an answer Use characterAtIndex:. However, this returns a Unicode character (unichar). If… May 11, 2026 at 11:32 pm
  • Editorial Team
    Editorial Team added an answer Flash is very limited to what HTML it can render… May 11, 2026 at 11:32 pm

Related Questions

After wasting two days with this question (and trying to make it work), I've
I'm developing an Windows Forms application that requires me to call a separate program
I have the following code: using System; using System.Collections.Generic; using System.Linq; using System.Text; using
There is a data file and some image files that I have to download

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.