Meshbeyn / Projects

namespace NTFSExtensions

public static class Compression

SetSparseAttribute

Mark or unmark any data stream on NTFS volume as sparse.

Signatures

Parameters

this FileInfo File
A FileInfo object as a reference to file. You may call this method directly from this object, because this is an extension method.
this FileStream File
A FileStream object as a reference to opened stream. You may call this method directly from this object, because this is an extension method.
SafeFileHandle FileStream
Handle to openned stream. You can become this handle from file stream object or using WinAPI function CreateFile.
bool Sparse
Set or unset the Compressed attribute

Remarks

Using the SetSparseAttribute method you can mark or unmark any unlocked stream on NTFS volume as a sparse stream. Unlike most other attributes, the "sparse" can be changed only using a special WinAPI call.

After setting this flag you may free some regions of a stream using method SetZeroData. If the cleared region contains removable clusters, this clusters will be released, so the file will occupy less space.

Before unsetting this flag you shall fill all cleared regions. Use method RefillZeroRegions to refill all regions before unset this flag. You can unset this flag only on systems Windows Vista+. On older systems you should rewrite the stream contents to new file or stream.

Attribute changing will fail if you have not sufficient access rights or if the file is in use.

All this methods call WinAPI function, that needs a file handle. So, the most fast and safe overload is the one with SafeFileHandle.

Example

Example: working with sparse streams

See also