private async Task UploadFileAsync(FluentFTP.FtpClient client, string remotePath, StorageFile localFile, CancellationToken token, IProgress <double> progress) { await semaphore.WaitAsync(); try { string remoteDirectory = Path.GetDirectoryName(remotePath); string remoteFileName = Path.GetFileName(remotePath); while (!(await client.DirectoryExistsAsync(remoteDirectory))) { string dir = Path.GetFileName(remoteDirectory); remoteDirectory = Path.GetDirectoryName(remoteDirectory); remoteFileName = Path.Combine(dir, remoteFileName); } await client.SetWorkingDirectoryAsync(remoteDirectory); using (var stream = await localFile.OpenStreamForReadAsync()) { await client.UploadAsync(stream, remoteFileName, FluentFTP.FtpExists.Overwrite, true, token, progress); } } finally { client.Dispose(); semaphore.Release(); } }