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 7625023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:08:13+00:00 2026-05-31T05:08:13+00:00

For certain reasons, I have to create a 1024 kb .txt file. Below is

  • 0

For certain reasons, I have to create a 1024 kb .txt file.

Below is my current code:

int size = 1024000 //1024 kb..


byte[] bytearray = new byte[size];
foreach (byte bit in bytearray) 
{
     bit = 0;
}


string tobewritten = string.Empty;
foreach (byte bit in bytearray) 
{
     tobewritten += bit.ToString();
}


//newPath is local directory, where I store the created file
using (System.IO.StreamWriter sw = File.CreateText(newPath))
{
     sw.WriteLine(tobewritten);
}

I have to wait at least 30 minutes to execute this piece of code, which I consider too long.

Now, I would like to ask for advice on how to actually achieve my mentioned objective effectively. Are there any alternatives to do this task? Am I writing bad code? Any help is appreciated.

  • 1 1 Answer
  • 0 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. Editorial Team
    Editorial Team
    2026-05-31T05:08:14+00:00Added an answer on May 31, 2026 at 5:08 am

    There are several misunderstandings in the code you provided:

    byte[] bytearray = new byte[size];
    foreach (byte bit in bytearray) 
    {
         bit = 0;
    }
    

    You seem to think that your are initializing each byte in your array bytearray with zero. Instead you just set the loop variable bit (unfortunate naming) to zero size times. Actually this code wouldn’t even compile since you cannot assign to the foreach iteration variable.

    Also you didn’t need initialization here in the first place: byte array elements are automatically initialized to 0.

    string tobewritten = string.Empty;
    foreach (byte bit in bytearray) 
    {
         tobewritten += bit.ToString();
    }
    

    You want to combine the string representation of each byte in your array to the string variable tobewritten. Since strings are immutable you create a new string for each element that has to be garbage collected along with the string you created for bit, this is relatively expensive, especially when you create 2048000 one of them – use a Stringbuilder instead.

    Lastly all of that is not needed at all anyway – it seems you just want to write a bunch of “0” characters to a text file – if you are not worried about creating a single large string of zeros (it depends on the value of size whether this makes sense) you can just create the string directly to do this one go – or alternatively write a smaller string directly to the stream a bunch of times.

    using (var file = File.CreateText(newpath))
    {
        file.WriteLine(new string('0', size));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For certain controls, the only option I have in Expression Blend is to create
An Orders table has a CustomerId column and an OrderId column. For certain reasons
I know there are LOTS of reasons why you would compose a certain object
In certain types of code (e.g. simulation/ games/ optimization), many physical quantities/properties are stored,
After certain R#-recommended edits R# colors the background of blocks of code in a
I have a panel in extjs-3.x. On a certain event(say click of a button),
EDIT 3: I am maintaining the original question below for historical reasons. However, I
everytime i create one jar file with the same contents of the last one
I have been trying to create my own base Models and Collections for extending
I have some classes that all implement a certain interface. I want to store

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.