WebDAV Drive Sample in .NET, C#

This sample implements a virtual file system that displays documents from a WebDAV server. You can edit documents, upload and download documents as well as manage folders structure using Windows File Manager. This sample provides automatic documents locking for Microsoft Office and AutoCAD documents. It supports synchronization, on-demand loading, selective offline files support, upload and download progress. It synchronizes files and folders both from a WebDAV server to the local user file system and from the local user file system to the WebDAV server. This sample is written in .NET Core, C#.  

You can use this sample out-of-the-box to manage documents on a WebDAV server, or you can use it as a starting point for your custom virtual drive to create OneDrive-like features for your DMS/CRM/ERP and reprogram it to publish data from your storage. 

This sample is supplied as part of the SDK with IT Hit WebDAV Client Library for .NET and with IT Hit User File System.

You can download this sample and trial licenses in the IT Hit WebDAV Client Library product download area and in the IT Hit User File System product download area. You can also clone it or browse the code on GitHub

Requirements

  • .NET Core 5.0 or later.
  • Microsoft Windows 10 Creators Update or later version.
  • NTFS file system.
  • The sample supports WebDAV servers with cookies authentication, Basic, Digest, NTLM, and Kerberos authentication.
  • To connect to WebDAV servers with cookies authentication Microsoft Edge is required. 

Configuring the Sample

To specify the WebDAV server URL edit the "WebDAVServerUrl" parameter in appsettings.json. This could be either a server root path (https://server/) or a WebDAV folder on your server (https://server/dav/).

For testing and demo purposes you can use one of the IT Hit demo servers. Navigate to https://webdavserver.net or // https://webdavserver.com in a web browser. Copy the URL or your test folder, that looks like https://webdavserver.net/User123456/ and specify it in the WebDAVServerUrl parameter.

By default, this sample will mount the user file system under the %USERPROFILE%\DAV\ folder (typically C:\Users\<username>\DAV\). To specify a different folder edit the "UserFileSystemRootPath" parameter in appsettings.json.

Setting the License

Note that to use the sample you need both the IT Hit WebDAV Client Library license and IT Hit User File System license.

To run the example, you will need both IT Hit WebDAV Client Library for .NET license and IT Hit User File System Engine for .NET License. You can download a trial license in the IT Hit WebDAV Client Library product download area and in the IT Hit User File System product download area. Note that this sample is fully functional with a trial license and does not have any limitations. The trial licenses are valid for one month will stop working after this. You can check the expiration date inside the license file. Download the licenses file and specify license strings in the WebDAVClientLicense and UserFileSystemLicense fields respectively in appsettings.json file.

You can also run the sample without explicitly specifying a license for 5 days. In this case, the Engine will automatically request the trial licenses from the IT Hit website https://www.userfilesystem.com. Make sure it is accessible via firewalls if any. After 5 days the Engine will stop working. To extend the trial period you will need to download trial licenses and specify them in appsettings.json

Running the Sample

To run the sample open the project in Visual Studio and run the project in debug mode. In the debug mode this sample provides additional support for the development and testing convenience. When starting in the debug mode, it will automatically create a folder in which the virtual file system will reside, register the user file system with the platform and then open an instance of Windows File Manager with a mounted file system as well as will launch a default web browser navigating to the WebDAV server URL specified in your appsettings.json:

WebDAV Drive sample launches Windows File manager with mounted virtual file system displaying content of your WebDAV server

You can start managing and editing files on your WebDAV server and will see all changes being propagated to the file system on the client machine. You can also edit documents and manage file structure on the client and all changes will automatically be reflected on the WebDAV server.

This sample provides a Windows Application Packaging Project which allows deployment of your application to the Windows Store, simplifies COM components debugging (thumbnails handler and Windows Explorer context menu), as well as your application can be installed without admin privileges. The package can be also used for direct deployment to users.

To start the project with thumbnails and context menu support follow these steps:

  1. Set the packaging project as your startup project.
  2. Set the VirtualDrive project under the packaging project as an Entry Point.

Run the project from Visual Studio. This will automatically register COM components as well as you can start debugging COM components without additional steps.

The packaging project will also perform an automatic cleanup on uninstall. Your sync root registration will be automatically unregistered, folders created by the application will be deleted as well as all COM components unregistered. 

On-Demand Loading

Initially, when you start the application, the user file system does not contain any file of folder placeholders, except the sync root folder. The content of the folders is populated only when any application is listing folder content. The content of files is loaded only when an application is opening a file for reading or writing.

After running the sample all files and folders are marked with a cloud iconOffline File, which means that the content of this file or folder is not available locally but instead resides in the remote location. Even though the file shows the correct size in the Size column, the file occupies zero bytes on the disk. You can open the file Properties dialog to check the "Size on disk" value. You can see in the console log that only root folder files and folders placeholders are being created when Windows File Manager listed the root folder content during the initial launch. Folders located deeper in the hierarchy are not loaded until their content is being requested by the platform file system calls. 

When any application is accessing the file, located under the \DAV\ folder, opening it for reading or writing, the operating system redirects the call to this sample, which loads file content from the WebDAV server. The file becomes marked with a green check-mark on a white background iconLocal File, which means the file content is present on the local disk:

Local Cloud File. Green check-mark on a white background icon means the file content is present on the local disk.

The Windows File Manager provides the "Always keep on this device" and "Free up space" context menus, which are standard menus provided by Windows OS. If you select the "Always keep on this device" the file or entire folder structure will be recursively loaded to the local disk, all file content will be loaded to the local disk and will become available offline. All files and folders are marked with a pinned file iconPinned File. Pinned files will not be deleted from the drive even if it runs low on space.

Always keep on this device menu will load all files to a loacal disk and will keep them from purging on low disk space.

To remove content from the local disk select the "Free up space" menu. It will restore the cloud iconOffline File.

When a large file is being downloaded from the WebDAV server, this sample submits progress reports to the operation system, to show a standard Windows "Downloading" dialog. At the same time, the Windows File Manager also shows progress in the files list view:

Large file download from the remote storage show progress reports in Windows File Manager and Files View

Microsoft Office and AutoCAD Documents Editing

This sample supports synchronization of the Microsoft Office and AutoCAD documents, avoiding the creation of temporary files in the remote storage as well as it does not rename or delete the document in your remote storage during MS Office/AutoCAD transactional save operation, preserving all data associated with a file in your remote storage.

This sample automatically locks the Microsoft Office and AutoCAD document in the remote storage when a document is being opened for editing and automatically unlocks the document when the file is closed. When the document is opened you will see the lock icon Lock icon in the Status column in Windows File Manager:

WebDAV Drive automatically locks Microsoft office document when it is being opened for editing and unlocks when closed.

The information about the lock (lock-token, etc.) is being saved on the client machine. When a document is modified on the client, all changes to the document are being saved on the local drive, without being sent to the WebDAV server until the file is unlocked. When the document is closed, the file content is being automatically sent to the server. Finally, the document is automatically unlocked

Any temporary Microsoft Office and AutoCAD documents (~$docfile.docx, G57BURP.tmp, etc) are stored in the local file system only and are NOT synchronized to the server. Typically temporary Microsoft Office and AutoCAD documents are being automatically deleted by Microsoft office when the document editing is completed.

Locks and eTags are being stored in the application data folder, under the path that corresponds with the mounted drive. For example: C:\Users\<user>\AppData\Local\WebDAV Drive\C\Users\<user>\DAV\ServerData\

File ETags

When listing folder content the client reads file eTags and saves them in the local file system. When sending modified content to the server, the client attaches the saved eTag to the request. The server compares eTags to ensure the file was not modified on the server since the file was read by the client. In the case of Microsoft Office documents, eTags provide one more level of protection against overwriting server changes in addition to locks.

In-Sync Status and ETag Usage For Synchronization

Synchronization in this sample is based on In-Sync file status as well as on the eTag received from the server.

The server to client synchronization is performed only if the file on the client is marked as In-Sync with the server (it is marked withIn-Sync icon or Pinned file or Cloud fileicon).

When any file or folder on the client is updated, it is marked as not In-SyncNot in sync icon, which means the content must be sent to the server. When the updated file is being sent from client to server, the server compares the eTag sent from the client with the ETag stored on the server, to avoid the server changes being overwritten. The file is updated only if the eTags match. Otherwise, the file/folder is considered to be in conflict and marked with the conflict icon.

Commands

For debugging and development purposes this sample provides the following console commands:

  • 'Esc' - simulates app uninstall. The sync root will be unregistered and all files will be deleted.
  • 'Space' - simulates machine reboot or application failure. The application exits without unregistering the sync root. All files and folders placeholders, their attributes, and states remain in the user file system. 
  • 'e' - starts/stops the Engine and all sync services. When the Engine is stopped, the user can still edit hydrated documents but can not hydrate files, access offline folders, or sync with remote storage.
  • 's' - starts/stops full synchronization service. The full synchronization service periodically syncs remote storage and user file system based on each file eTag and in-sync status.   
  • 'm' - starts/stops remote storage monitor. When the remote storage monitor is stopped the sample does not receive notifications from the remote storage.
  • 'l' - opens a log file.
  • 'b' - opens Help & Support portal to submit support tickets, report bugs, suggest features.

See Also:

Next Article:

WebDAV Client iOS Sample in C#, Xamarin