WebDAV Drive Sample for Windows in .NET, C#

This sample implements a virtual file system for Windows 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 as well as 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, 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 6 or later or .NET Framework 4.8.
  • 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. 

Server Requirements

This sample requires WebDAV server with collections synchronization support. The following samples support collections synchronization:

You can also test this sample with IT Hit demo servers:

Setting 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. Set the license content directly as a value (NOT as a path to the license file). Do not forget to escape quotes: \":

"UserFileSystemLicense": "<?xml version=\"1.0\" encoding=\"utf-8\"?><License…

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

Setting WebDAV Server URL

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 to 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.

"WebDAVServerUrl": "https://webdavserver.net/User123456",

Setting Web Sockets Server URL

The client application receives notifications from server about changes via web sockets. To specify your web sockets server URL edit the "WebSocketServerUrl" parameter in appsettings.json.

In case you are using IT Hit demo servers, you will specify the demo server root, without user folder: wss://webdavserver.net or wss://webdavserver.com. 

"WebSocketServerUrl": "wss://webdavserver.net"

Setting User File System Root Folder

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.

"UserFileSystemRootPath": "%USERPROFILE%\\DAVv7\\",

Note that this folder must be indexed, otherwise the file system may not work as expected. This is a Microsoft Windows API requirement for cloud file systems.

Running the Sample

To run the sample open the WebDAVDrive 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 where 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.

Starting with IT Hit User File System v5 Beta, the WebDAVDrive project supports identity and provides the same functionality as WebDAVDrive.Packaging project. Starting WebDAVDrive project directly registers thumbnails handler shell extension, context menu handler and custom states & columns handler.

This sample provides a Windows Application Packaging Project which allows deployment of your application to the Windows Store. The package can be also used for direct deployment to users in a corporate environment or consumer environment. Start reading about various deployment scenarios in this article.

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, preserving all data associated with a file in your remote storage.

This sample automatically locks Microsoft Office and AutoCAD documents in the remote storage when a document is being opened for editing and automatically unlocks when the document 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.

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.

File ETags

When listing folder content the client reads file eTags and stores it as part of each placeholder. 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 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.
  • 'd' - enable/disable debug and performance logging.
  • 'l' - opens a log file.
  • 'b' - opens Help & Support portal to submit support tickets, report bugs, suggest features.

See Also:

Next Article:

WebDAV Drive Sample for macOS in .NET, C#