By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,361 Members | 1,683 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,361 IT Pros & Developers. It's quick & easy.

How to create new file with fixed size

P: n/a
Hi

For some purposes, I need to create a new binary file and to allocate
a fixed length for that file on the disk.
I need something like File.Create(string fileName, long
initialSizeInBytes).
Is there a framework/system method to do that or should I create the
file and then fill it until the size is reached?

Thanks

Aug 31 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On 31 , 12:59, nano2k <adrian.rot...@ikonsoft.rowrote:
Hi

For some purposes, I need to create a new binary file and to allocate
a fixed length for that file on the disk.
I need something like File.Create(string fileName, long
initialSizeInBytes).
Is there a framework/system method to do that or should I create the
file and then fill it until the size is reached?

Thanks
System.IO.File.WriteAllBytes("file.txt", new byte[100]);

Regards,
Mykola
http://marss.co.ua

Aug 31 '07 #2

P: n/a
On 31 Aug, 13:09, marss <marss...@gmail.comwrote:
On 31 , 12:59, nano2k <adrian.rot...@ikonsoft.rowrote:
Hi
For some purposes, I need to create a new binary file and to allocate
a fixed length for that file on the disk.
I need something like File.Create(string fileName, long
initialSizeInBytes).
Is there a framework/system method to do that or should I create the
file and then fill it until the size is reached?
Thanks

System.IO.File.WriteAllBytes("file.txt", new byte[100]);

Regards,
Mykolahttp://marss.co.ua
Hi Mykola
Thanks for your response.
I'm using .NET Framework 1.1, so this method you suggested is not
available in my case.
The files I will create are far larger than you suggested in your
post. May be hundreds of megs, or even few gigs.
What you suggest is that I should create a file, than write the amount
of bytes I need. Writing in one shot is not ok. It's not my desire to
allocate a huge buffer just to tranfer it to a file.
I'd rather write to file successively a reasonable sized buffer until
the desired size is reached.
This is my approach at the moment, but I thought that there should be
a another, more optimum, way to do it.

Aug 31 '07 #3

P: n/a
Hello nano2k,

use this one
using (FileStream stream = new FileStream(path, FileMode.Create, FileAccess.Write,
FileShare.Read))
{
stream.Write(bytes, 0, bytes.Length);
}

u can do in blocks, writing to file several times

---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo
nOn 31 Aug, 13:09, marss <marss...@gmail.comwrote:
n>
>On 31 , 12:59, nano2k <adrian.rot...@ikonsoft.rowrote:
>>Hi

For some purposes, I need to create a new binary file and to
allocate
a fixed length for that file on the disk.
I need something like File.Create(string fileName, long
initialSizeInBytes).
Is there a framework/system method to do that or should I create the
file and then fill it until the size is reached?
Thanks
System.IO.File.WriteAllBytes("file.txt", new byte[100]);

Regards,
Mykolahttp://marss.co.ua
nHi Mykola
nThanks for your response.
nI'm using .NET Framework 1.1, so this method you suggested is not
navailable in my case.
nThe files I will create are far larger than you suggested in your
npost. May be hundreds of megs, or even few gigs.
nWhat you suggest is that I should create a file, than write the
namount
nof bytes I need. Writing in one shot is not ok. It's not my desire to
nallocate a huge buffer just to tranfer it to a file.
nI'd rather write to file successively a reasonable sized buffer until
nthe desired size is reached.
nThis is my approach at the moment, but I thought that there should be
na another, more optimum, way to do it.
Aug 31 '07 #4

P: n/a
On Fri, 31 Aug 2007 02:59:18 -0700, nano2k <ad***********@ikonsoft.ro>
wrote:
>Hi

For some purposes, I need to create a new binary file and to allocate
a fixed length for that file on the disk.
I need something like File.Create(string fileName, long
initialSizeInBytes).
Is there a framework/system method to do that or should I create the
file and then fill it until the size is reached?

Thanks


FileStream fs=new FileStream("???", FileMode.Create);
long len=....
fs.SetLength(len);

Note that the contents of the file are *not* guranteed to be 0.

Austin
Aug 31 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.