Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create contiguous file using C#?

Tags:

c#

.net

file-io

Is it possible to create a large (physically) contiguous file on a disk using C#? Preferably using only managed code, but if it's not possible then an unmanaged solution would be appreciated.

like image 248
tmitchel2 Avatar asked Apr 15 '26 10:04

tmitchel2


2 Answers

Any file you create will be logically contiguous.

If you want physically contiguous you are on OS and FS territory. Really (far) beyond the control of normal I/O API's.

But what will probably come close is to claim the space up-front: create an Empty stream and set the Length or Position property to what you need.

like image 112
Henk Holterman Avatar answered Apr 18 '26 00:04

Henk Holterman


Writing a defragger?

It sounds like you're after the defragmentation API anyway:-

http://msdn.microsoft.com/en-us/library/aa363911%28v=vs.85%29.aspx

The link from the bottom, cos it seems you've missed the C# wrapper that someone has kindly produced.

http://blogs.msdn.com/b/jeffrey_wall/archive/2004/09/13/229137.aspx


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!