Solved

Create file with a specific size..

Posted on 2003-11-05
4
385 Views
Last Modified: 2007-12-19
Hi, I want to create a file with a specific size. I wrote this code:

string FILE_NAME = @"C:\test.data";
FileStream fs = new FileStream(FILE_NAME, FileMode.CreateNew);
BinaryWriter b = new BinaryWriter(fs);

const uint _byte = 1024;
ulong MB = (_byte * _byte) * UInt64.Parse(textBox1.Text.ToString()); //for example: 30
byte[] buffer = new byte[MB];
b.Write(buffer);
fs.Close();
b.Close();

..and I have some questions:
Is this a bad way to do it?
How big file can I create this way?
Can I optimize the speed?

Thanks!
0
Comment
Question by:jooon
  • 2
4 Comments
 
LVL 15

Expert Comment

by:SRigney
ID: 9692068
The file can be as large as you want it.  I would recommend that whatever size you decide to make it, you write it in blocks of a fixed size multiple times until it's the size desired.

Is this a bad way to do it?  
That's a question for the ages.  If it accomplishes the goal in a not too timely fashion then it's a good way to do it.

Can I optimize the speed?
That will depend on specifically what you are doing.  With the existing code it doesn't appear that there is any optimization left, but if you are doing other things as well there may be some optimization.  Also the block size that you use to write to the file.  A large enough number to not use too much RAM, but a small enough number to work on any machine that you plan on deploying the application to.  Depending on what the total file size is supposed to be I would break it into chunks not greater than 1 MB for each write.  There's probably some optimization to going even smaller (256K or 64K) depending on what can get through the Cache quickly.
0
 
LVL 20

Accepted Solution

by:
TheAvenger earned 50 total points
ID: 9692673
ulong MB = (_byte * _byte) * UInt64.Parse(textBox1.Text.ToString()); //for example: 30
byte[] buffer = new byte[MB];

This is not very effective as it creates first a large empty buffer in memory and then writes it to the file. This can also limit the size of file you can create. So follow the advice of SRigney and make it write in several blocks of let's say 100 Kb or 1 Mb.

In order to optimize the speed, use a BufferedStream instead of the BinaryWriter. Then you can use a very small buffer so you can create a loop like the one that follows and still have a very good speed as the BufferedStream will make disk operations only when it's buffer is full. This will change performance significantly if you use files bigger than a couple of Mbs.

ulong MB = UInt64.Parse(textBox1.Text.ToString()); //for example: 30
byte[] buffer = new byte[1024];
for (int i = 0; i < MB; i++)
  for (int j = 0; j < 1024; j++)
    b.Write (buffer);
0
 
LVL 15

Expert Comment

by:SRigney
ID: 9693653
Good suggestion Avenger
0
 

Author Comment

by:jooon
ID: 9695144
Thank you both!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now