How to create a virtual file system with offline files (similar to OneDrive) in .NET?

2.7k views Asked by At

I need to create a virtual file system that syncs files from my Azure app service to local drive and backward. I need only some files and folders to be synchronized to my local file system and be available offline. The files/folders should synchronize during first access or when the user selects the "Always keep on this device" menu. Similar to how OneDrive is doing. I found the Microsoft CloudMirror C++ sample which has some basic OneDrive functionality, but it syncs all files one time during start and no listing/loading on first access.

Is it possible to build virtual file system with above functionality completely in .NET?

1

There are 1 answers

2
Dim Norin On BEST ANSWER

To build an OneDrive-like client you need to create the Sync Provider using cloud filter API, which is mostly programmable via Win32 (with some exceptions, see below), but you can build a complete OneDrive analog in .NET, including "Always keep on this device" menu and on-demand loading.

Here is the functionality that is available via .NET, so you do not need to PInvoke it:

Here is functionality available via Win32 functions only, you will need to call it via PInvoke:

Here is what you can do to write all code in .NET:

  • One option is to import all required Win32 functions using extern to .NET, for example:

     [DllImport("cldapi.dll", SetLastError = true, ExactSpelling = true)]
     public static extern int CfGetPlaceholderStateFromFileInfo(IntPtr infoBuffer, FILE_INFO_BY_HANDLE_CLASS infoClass);
    
     [DllImport("cldapi.dll", SetLastError = true, ExactSpelling = true)]
     public static extern HRESULT CfSetPinState(IntPtr fileHandle, int pinState, int pinFlags, IntPtr overlapped);
    
  • Another option could be using this sample.

Some more notes on building an OneDrive-like file system:

  • The "Always keep on this device"/"Free up space" shows automatically on sync root registration. But it only sets Pinned and Unpinned file attributes. You need to monitor the file system, examine the pinned/unpinned attributes, and hydrate/dehydrate each file.
  • The on-demand folders listing is done inside the CF_CALLBACK_TYPE_FETCH_PLACEHOLDERS callback. You need to register callback using CfConnectSyncRoot() and listen to it in .NET code.
  • The on-demand file content download (hydration) is done inside the CF_CALLBACK_TYPE_FETCH_DATA callback. You need to register and listen to it in .NET code.