Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / August 2007

Tip: Looking for answers? Try searching our database.

How to create new file with fixed size

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
nano2k - 31 Aug 2007 10:59 GMT
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
marss - 31 Aug 2007 11:09 GMT
> Hi
>
[quoted text clipped - 6 lines]
>
> Thanks

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

Regards,
Mykola
http://marss.co.ua
nano2k - 31 Aug 2007 11:54 GMT
> > Hi
>
[quoted text clipped - 11 lines]
> 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.
Michael Nemtsev, MVP - 31 Aug 2007 13:25 GMT
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

n> On 31 Aug, 13:09, marss <marss...@gmail.com> wrote:
n>

>>> Hi
>>>
[quoted text clipped - 11 lines]
>> 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
Austin Ehlers - 31 Aug 2007 15:57 GMT
>Hi
>
[quoted text clipped - 6 lines]
>
>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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.