How to create new file with fixed size

  • Thread starter Thread starter nano2k
  • Start date Start date
N

nano2k

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
 
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
 
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?

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.
 
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


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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top