Create files with preallocated disk space

  • Thread starter Thread starter SpookyET
  • Start date Start date
S

SpookyET

How would you create files with a certain size in .NET? FileInfo.Create
does not have a length parameter. P/Invoke?
 
SpookyET said:
How would you create files with a certain size in .NET? FileInfo.Create
does not have a length parameter. P/Invoke?

Probably. Do you want to preallocate the space or do you want sparse files?
 
Daniel O'Connell said:
Probably. Do you want to preallocate the space or do you want sparse
files?

Err, I guess that was a touch vauge. You can either preallocate the space on
disk or you can use sparse files which create a file that *appears* to be of
size X but has no actual disk backing. That allows you to save space when
there will be times with large parts of the file with no data, however it
only works on NTFS and probably increases the chances of fragmentation
considerably.

I assume you are still working on your BT client, if so I'd advise offering
both. I made a mistake originally in my answer anyway.
FileStream::SetLEngth should do what you need if you just want to
preallocate space, but if you want sparse files you will have to use
PInvoke.
 
Thank you very much. I don't know how I have missed SetLength. Sleeping
2-3 hours a night for the past 3 days could have been the major factor.
I'm not interested in sparse files since they do not have any advantage
over incrementing files besides showing the size that they will have when
the data is complete.
 
SpookyET said:
Thank you very much. I don't know how I have missed SetLength. Sleeping
2-3 hours a night for the past 3 days could have been the major factor.
I'm not interested in sparse files since they do not have any advantage
over incrementing files besides showing the size that they will have when
the data is complete.

Well, the main advantage is you don't have to write code to get incremental
stuff right. You jsut read from\write to the file at the right location.
 
As you have said, it only works on NTFS. There are plenty who still use
Win98. Also, I might want to make it available to the penguin people
too. Since Linux has a few file systems, that would complicate things too
much. I'll have to deal with incremental files myself.
 
SpookyET said:
As you have said, it only works on NTFS. There are plenty who still use
Win98. Also, I might want to make it available to the penguin people
too. Since Linux has a few file systems, that would complicate things too
much. I'll have to deal with incremental files myself.
LOL, yes, there is that. I'm pretty sure some of the Linux filesystems
support sparse files as well, but it'd be a different pinvoke call. And, at
that, finding out what system you are running on is actually rather
difficult from what I understand. Last I heard, Mono claims to be running on
windows XP, no matter what system you are actually running on.

In that case, SetLength should be sufficent.
SpookyET said:
Thank you very much. I don't know how I have missed SetLength.
Sleeping
2-3 hours a night for the past 3 days could have been the major factor.
I'm not interested in sparse files since they do not have any advantage
over incrementing files besides showing the size that they will have
when
the data is complete.

Well, the main advantage is you don't have to write code to get
incremental
stuff right. You jsut read from\write to the file at the right location.
On Tue, 11 May 2004 19:56:48 -0500, Daniel O'Connell [C# MVP]


message
How would you create files with a certain size in .NET?
FileInfo.Create
does not have a length parameter. P/Invoke?

Probably. Do you want to preallocate the space or do you want sparse
files?

Err, I guess that was a touch vauge. You can either preallocate the
space on
disk or you can use sparse files which create a file that *appears* to
be of
size X but has no actual disk backing. That allows you to save space
when
there will be times with large parts of the file with no data, however
it
only works on NTFS and probably increases the chances of fragmentation
considerably.

I assume you are still working on your BT client, if so I'd advise
offering
both. I made a mistake originally in my answer anyway.
FileStream::SetLEngth should do what you need if you just want to
preallocate space, but if you want sparse files you will have to use
PInvoke.
 
Mono has always worked on Windows.

SpookyET said:
As you have said, it only works on NTFS. There are plenty who still use
Win98. Also, I might want to make it available to the penguin people
too. Since Linux has a few file systems, that would complicate things
too
much. I'll have to deal with incremental files myself.
LOL, yes, there is that. I'm pretty sure some of the Linux filesystems
support sparse files as well, but it'd be a different pinvoke call. And,
at
that, finding out what system you are running on is actually rather
difficult from what I understand. Last I heard, Mono claims to be
running on
windows XP, no matter what system you are actually running on.

In that case, SetLength should be sufficent.
Thank you very much. I don't know how I have missed SetLength.
Sleeping
2-3 hours a night for the past 3 days could have been the major
factor.
I'm not interested in sparse files since they do not have any
advantage
over incrementing files besides showing the size that they will have
when
the data is complete.

Well, the main advantage is you don't have to write code to get
incremental
stuff right. You jsut read from\write to the file at the right
location.

On Tue, 11 May 2004 19:56:48 -0500, Daniel O'Connell [C# MVP]


message
How would you create files with a certain size in .NET?
FileInfo.Create
does not have a length parameter. P/Invoke?

Probably. Do you want to preallocate the space or do you want sparse
files?

Err, I guess that was a touch vauge. You can either preallocate the
space on
disk or you can use sparse files which create a file that *appears*
to
be of
size X but has no actual disk backing. That allows you to save space
when
there will be times with large parts of the file with no data,
however
it
only works on NTFS and probably increases the chances of
fragmentation
considerably.

I assume you are still working on your BT client, if so I'd advise
offering
both. I made a mistake originally in my answer anyway.
FileStream::SetLEngth should do what you need if you just want to
preallocate space, but if you want sparse files you will have to use
PInvoke.
 
SpookyET said:
Mono has always worked on Windows.

I know, but System.Environment.OSVersion, atleast at some point, was
reporting windows XP 2600 for linux systems. I can't say with any certainty
that it still is, but that was happening at some point IIRC.
 
Back
Top