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

  • Home
  • SEARCH
  • 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 45733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:51:20+00:00 2026-05-10T15:51:20+00:00

I am creating a downloading application and I wish to preallocate room on the

  • 0

I am creating a downloading application and I wish to preallocate room on the harddrive for the files before they are actually downloaded as they could potentially be rather large, and noone likes to see ‘This drive is full, please delete some files and try again.’ So, in that light, I wrote this.

// Quick, and very dirty System.IO.File.WriteAllBytes(filename, new byte[f.Length]); 

It works, atleast until you download a file that is several hundred MB’s, or potentially even GB’s and you throw Windows into a thrashing frenzy if not totally wipe out the pagefile and kill your systems memory altogether. Oops.

So, with a little more enlightenment, I set out with the following algorithm.

using (FileStream outFile = System.IO.File.Create(filename)) {     // 4194304 = 4MB; loops from 1 block in so that we leave the loop one      // block short     byte[] buff = new byte[4194304];     for (int i = buff.Length; i < f.Length; i += buff.Length)     {         outFile.Write(buff, 0, buff.Length);     }     outFile.Write(buff, 0, f.Length % buff.Length); } 

This works, well even, and doesn’t suffer the crippling memory problem of the last solution. It’s still slow though, especially on older hardware since it writes out (potentially GB’s worth of) data out to the disk.

The question is this: Is there a better way of accomplishing the same thing? Is there a way of telling Windows to create a file of x size and simply allocate the space on the filesystem rather than actually write out a tonne of data. I don’t care about initialising the data in the file at all (the protocol I’m using – bittorrent – provides hashes for the files it sends, hence worst case for random uninitialised data is I get a lucky coincidence and part of the file is correct).

  • 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. 2026-05-10T15:51:21+00:00Added an answer on May 10, 2026 at 3:51 pm

    FileStream.SetLength is the one you want. The syntax:

    public override void SetLength(     long value ) 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 62k
  • Answers 62k
  • 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
  • added an answer Converting a Generating Function to an implementation that actually spits… May 11, 2026 at 10:15 am
  • added an answer Some of my rules of thumb: Index ALL primary keys… May 11, 2026 at 10:15 am
  • added an answer I think this is precisely what you are asking for:… May 11, 2026 at 10:15 am

Related Questions

I am creating a downloading application and I wish to preallocate room on the
I am creating a small modal form that is used in Winforms application. It
I am creating a program that will be installed using the .net installer project.
I am creating a GUI for a machine that runs remote (WinXP) or on
I am creating a Windows Service in C# that processes messages from a queue.
I am creating a windows service and want to know best practices for this.
I am creating a Rails plugin and it is dynamically adding a method to
I am creating a website in CakePHP and I am kind of new on
I am creating a generic error handling / logging class for our applications. The
I am creating a Windows Forms control derived from UserControl to be embedded in

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.