Click or drag to resize

IFolderAsyncCreateFileAsync Method (String, String)

IT Hit WebDAV Classes Reference
Creates a file with specified name.

Namespace:  ITHit.WebDAV.Client
Assembly:  ITHit.WebDAV.Client (in ITHit.WebDAV.Client.dll) Version: 2.0.420.0
Syntax
Task<IFileAsync> CreateFileAsync(
	string name,
	string lockToken
)

Parameters

name
Type: SystemString
Name of the new file.
lockToken
Type: SystemString
Lock token for this folder.

Return Value

Type: TaskIFileAsync
Newly created file.
Exceptions
ExceptionCondition
MethodNotAllowedExceptionItem with specified name already exists.
ForbiddenExceptionCreation of child items not allowed.
NotFoundExceptionThis folder doesn't exist on the server.
LockedExceptionThis folder is locked and no or invalid lock token was specified.
WebDavHttpExceptionServer returned unknown error.
WebDavExceptionUnexpected error occurred.
Remarks
If a file with a specified name exists it's content is truncated.
Examples
string license = "<?xml version='1.0' encoding='utf...
WebDavSessionAsync session = new WebDavSessionAsync(license);
session.Credentials = new NetworkCredential("User1", "pwd");
IFolderAsync folder = await session.OpenFolderAsync(new Uri("http://server:8080/Sales"));
FileInfo file = new FileInfo("C:\\Products.exe");
LockInfo lockInfo = await folder.LockAsync(LockScope.Exclusive, true, "User 1", new TimeSpan(0, 30, 0));
IFileAsync file = await folder.CreateFileAsync(file.Name, lockInfo.LockToken.LockToken);
using (Stream webStream = await file.GetWriteStreamAsync(
    "application/octet-stream", file.Length, lockInfo.LockToken.LockToken))
{
    int bufSize = 1048576; // 1Mb
    byte[] buffer = new byte[bufSize];
    int bytesRead = 0;

    using (FileStream fileStream = await file.OpenAsync(FileMode.Open, FileAccess.Read, FileShare.Read))
    {
        while ( (bytesRead = await fileStream.ReadAsync(buffer, 0, bufSize))>0)
           await webStream.WriteAsync(buffer, 0, bytesRead);
    }
}
folder.Unlock(lockInfo.LockToken.LockToken);
See Also