[Mono-dev] Cross-platform fsync()

Robert Jordan robertj at gmx.net
Tue Jan 9 20:33:38 EST 2007

Patrick Earl wrote:
> Greetings all.  I've recently run into the need for a cross platform
> fsync() call.  As far as I know, all of the flavors of unix that mono
> supports provide the fsync call themselves.  On windows, there is a
> _commit() function that does the same thing.  It seems like the
> underlying platform support is there, but I'm not sure how to bring
> that back up into the .net world so we can, for example, perform some
> sort of flush to disk operation on a FileStream.  Suggestions?  Ideas?

On MS.NET and Mono for Windows, the proper function is WIN32's
FlushFileBuffers()that can be called via p/invoke from kernel32.dll
either on FileStream.Handle, or (.NET 2.0) on FileStream.SafeFileHandle.

On Linux, FlushFileBuffers() is provided by Mono's WAPI layer
that calls fsync(2) internally, but I don't know how it can be
called from managed code. Maybe like this (untested):

public static bool FlushFileBuffers (IntPtr handle);

For .NET 2.0:
public static bool FlushFileBuffers (SafeFileHandle handle);

The meaning of "__Internal" is documented here:



More information about the Mono-devel-list mailing list