As the first step you need to create
WebDavFileobject using static Create method.
All supported methods are asynchronous. To use them you need to supply
asyncStateobject. This object serves for a single purpose - it is passed back in the callback. (See for example the State property). Supply
nullif you don't need this object.
All methods return a NetRequest instance. You can use this object to abort the operation.
File operations are one-shot operations. Respective callback is called once - upon the operation completion.
Use the Succeeded property to see if the operation completed successfully.
In case of recursive operations the callback is called after each recursion step. For more information see the description of individual methods.
Open remote directory
WebDavFile dir = WebDavFile.Create("https://myServer/xyz/MyBackupDir", "login", "password");
Debug.Assert( dir.IsDirectory ) ;
Enumeration of a remote directory
dir.StartEnum( delegate(NetEnumResult er)
{
NetFile file;
while ((file = er.GetNextFile()) != null)
Debug.WriteLine( "File name: {0}", file.Name ) ;
});
Download file
dir.Download( @"\MyDir\MyFile.x", delegate(NetAsyncResult ar)
{
if( ar.Succeeded )
;// use ar.Stream to save downloaded data
} );
Instead of anonymous delegate you can use explicit callback, for example
private void DefaultAsyncCallback(NetAsyncResult ar)
{
Debug.WriteLine("Request {0}:\n{1}", ar.Succeeded ? "Succeeded" : "Failed", ar.Response);
}
Rename
netFile.Rename( "NewFileName", false, DefaultAsyncCallback);
Delete
netFile.Delete( DefaultAsyncCallback, null);
Upload data
var stream = new MemoryStream( System.Text.Encoding.UTF8.GetBytes("Some text") );
dir.Upload(stream, "Test.txt", DefaultAsyncCallback);
Sample callback used for recursive download/upload
private void DefaultPartialAsyncCallback(NetAsyncResult ar)
{
if (ar is PartialNetAsyncResult)
{
// Called for directory upload/download
var res = (ar as PartialNetAsyncResult);
Debug.WriteLine( "{0} -> {1} : {2}", res.File.FullPath, res.LocalPath, (res.Succeeded ? "OK" : res.Response) );
if (!res.Succeeded)
res.Continue = false; // break the loop (we could also ignore the error and continue)
}
else
{
Debug.WriteLine("Request {0}:\n{1}", ar.Succeeded ? "Succeeded" : "Failed", ar.Response);
}
}
| Class | Description |
|---|---|
| EnumFolderLock | Supported lock object |
| EnumFolderLockEntry | The lock entry |
| EnumFolderProp | WebDAV property |
| EnumFolderPropStat | Property status object |
| EnumFolderResponse | WebDAV response object |
| EnumFolderRoot | XML Serialization root element |
| NetAsyncResult | Represents the result of the asynchronous operation performed on a NetFile object |
| NetEnumResult | Represents the result of the asynchronous remote directory enumeration |
| NetFile | Represents the file/directory object located on the remote server. |
| NetRequest | Represents the remote file system request |
| PartialNetAsyncResult | Represents the partial result of the asynchronous download/upload operation. It is passed to the NetAsyncCallback function in case of recursive operation after each file operation completion. |
| RecursiveDownloadRequest | Represents the recursive download request |
| RecursiveUploadRequest | Represents the recursive upload request |
| WebDavClient | Represents the client responsible for remote WebDAV server requests |
| WebDavEnumResult | Represents the result of the asynchronous WebDAV directory enumeration |
| WebDavFile |
WebDavFile class supports file operations on a remote WebDAV server. |
| Delegate | Description |
|---|---|
| NetAsyncCallback | References a method to be called when a corresponding asynchronous operation completes. |
| NetEnumCallback | References a method to be called when the asynchronous enumeration operation completes. |