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 documents. It supports synchronization, on-demand loading, selective offline files support, upload and download progress, and error reporting. It synchronizes files and folders both from a WebDAV server to the local user file system and from the local user file system to 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 a trial licenses in the IT Hit WebDAV Client Library product download area and in IT Hit User File System product download area. You can also clone it or browse the code on GitHub.
- .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.
- For servers with cookies authentication Microsoft Edge Canary is required.
If you have WebView2 Runtime installed on the client machine, you must remove it for the HTML login dialog to load in Microsoft Edge Canary.
Configuring the Sample
To specify the WebDAV server URL edit the "RemoteStorageRootPath" parameter in appsettings.json. This could be either server root path (https://server/) or a WebDAV folder on your server (https://server/dav/).
By default this sample will mount the user file system under the %USERPROFILE%\DAV\ folder (typically C:\Users\<username>\DAV\). To specify the a different folder edit the "UserFileSystemRootPath" parameter in appsettings.json.
Setting the License
Note that to use the sample you need both 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 licenses in the IT Hit WebDAV Client Library product download area and in 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 their content 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 a 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:
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 theWebDAV server.
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 icon, 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 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 icon, which 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 icon. Pinned files will not be deleted from the drive even if it runs low on space.
To remove content from the local disk select the "Free up space" menu. It will restore the cloud icon.
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:
Microsoft Office Documents Editing
When a Microsoft Office document is being opened, the WebDAV Drive sample automatically locks the document on a WebDAV server:
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 and than unlocked.
Any temporary Microsoft Office 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 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\
When listing folder content the client reads file ETags and saves them in 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 with or or icon). If the file is modified on the server and is marked as not In-Sync on the client, the files are considered to be in conflict and the conflict icon is displayed.
When any file or folder on the client is updated, it is marked as not In-Sync, 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 to be overwritten. The file/folder is updated only if the ETags match. Otherwise, the file/folder is considered to be in conflict and marked with the conflict icon.
Stopping the Sample
You can exit the sample running in the debug mode in 3 ways:
- If you press 'Q' the user file system will be unregistered and all files will be deleted. This simulates uninstall with complete cleanup.
- If you press 'q' the user file system will be unregistered. All files and folders downloaded on disk will be converted into regular files and folders. The offline files and folders will disappear. This simulates your application's uninstallation process.
- If you press any other key, the application will exit without unregistering the user file system. All files and folders placeholders, their attributes, and states remain in the user file system, even though most of the functionality, such as downloading file content or listing folder content for offline folders are unavailable. You can start this sample again to continue managing documents. This simulates the machine reboot or application failure.
- Creating Virtual File System in .NET Quick Start Guide
- Locking Files and Folders in Virtual File System