jooon
asked on
Create file with a specific size..
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!
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
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!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Good suggestion Avenger
ASKER
Thank you both!
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.