本文整理汇总了C#中IFile.OpenAsync方法的典型用法代码示例。如果您正苦于以下问题:C# IFile.OpenAsync方法的具体用法?C# IFile.OpenAsync怎么用?C# IFile.OpenAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IFile
的用法示例。
在下文中一共展示了IFile.OpenAsync方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ConvertAsync
private static async Task ConvertAsync(TopicData topic, IDictionary<string, TopicData> name2topic, Paths paths, IFile destFile, CancellationToken cancellationToken)
{
using (var destStream = await destFile.OpenAsync(FileAccess.ReadAndWrite, cancellationToken))
{
await ConvertAsync(topic, destStream, name2topic, paths, cancellationToken);
}
}
示例2: Init
private async Task Init(IFile storageFile)
{
var fileAsStream = await storageFile.OpenAsync(FileAccess.Read);
byte[] pbKey = await LoadXmlKeyFile(fileAsStream);
if (pbKey == null) pbKey = LoadKeyFile(fileAsStream.ToArray());
if (pbKey == null) throw new InvalidOperationException();
_keyData = pbKey;
}
示例3: DownloadFileAsync
// http://stackoverflow.com/questions/21169573/how-to-implement-progress-reporting-for-portable-httpclient
// var progress = new Microsoft.Progress<double>();
// progress.ProgressChanged += (sender, value) => System.Console.Write("\r%{0:N0}", value);
// var cancellationToken = new CancellationTokenSource();
// await DownloadFileAsync("http://www.dotpdn.com/files/Paint.NET.3.5.11.Install.zip", progress, cancellationToken.Token);
public async Task DownloadFileAsync(string url, ProgressBar_Model progressBar, IFile file,CancellationToken token)
{
HttpClient client = new HttpClient();
var response = await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead, token);
if (!response.IsSuccessStatusCode)
{
throw new Exception(string.Format("The request returned with HTTP status code {0}", response.StatusCode));
}
long total = response.Content.Headers.ContentLength.HasValue ? response.Content.Headers.ContentLength.Value : -1L;
bool canReportProgress = progressBar != null && total != -1;
if (total == -1 && progressBar != null) progressBar.IsIndeterminate = true;
using (Stream
stream = await response.Content.ReadAsStreamAsync(),
writestream = await file.OpenAsync(FileAccess.ReadAndWrite))
{
int totalRead = 0;
byte[] buffer = new byte[4096];
bool isMoreToRead = true;
do
{
token.ThrowIfCancellationRequested();
var read = await stream.ReadAsync(buffer, 0, buffer.Length, token);
if (read == 0)
{
isMoreToRead = false;
}
else
{
//var data = new byte[read];
//buffer.ToList().CopyTo(0, data, 0, read);
// TODO: put here the code to write the file to disk
await writestream.WriteAsync(buffer, totalRead, read);
totalRead += read;
if (canReportProgress)
{
progressBar.Value = Convert.ToInt32((totalRead * 1d) / (total * 1d) * 100);
}
}
} while (isMoreToRead);
}
}
示例4: Write
public async Task Write(PwDatabase databaseData, IFile databaseFile)
{
kdb4File = new Kdb4File(databaseData);
ResetHeaderBytes();
MemoryStream memStream = new MemoryStream();
BinaryWriter writer = new BinaryWriter(memStream);
_headerWriter.WriteHeaders(writer, kdb4File);
var header = memStream.ToArray();
var hashOfHeader = _hasher.Hash(header);
var outStream = new MemoryStream();
await outStream.WriteAsync(kdb4File.pbStreamStartBytes, 0, (int)kdb4File.pbStreamStartBytes.Length);
var configuredStream = ConfigureStream(outStream);
var persister = new Kdb4Persister(new CryptoRandomStream(CrsAlgorithm.Salsa20, kdb4File.pbProtectedStreamKey, _hasher));
var data = persister.Persist(databaseData.Tree, hashOfHeader);
try
{
await Task.Run(() =>
configuredStream.Write(data, 0, data.Length));
}
catch(Exception e)
{
}
configuredStream.Dispose();
var compressed = outStream.ToArray();
var keyGenerator = new KeyGenerator(
_hasher,
_keyTransformer,
databaseData.MasterKey,
databaseData.MasterKey.PercentComplete);
var aesKey = await keyGenerator.GenerateHashedKeyAsync(kdb4File.pbMasterSeed, kdb4File.pbTransformSeed, (int)databaseData.KeyEncryptionRounds);
var encrypted = await _databaseEncryptor.Encrypt(compressed, aesKey, kdb4File.pbEncryptionIV);
writer.Write(encrypted);
var streamToWriteToFile = writer.BaseStream;
streamToWriteToFile.Position = 0;
var bytesToWrite = streamToWriteToFile.ToArray();
var databaseStream = await databaseFile.OpenAsync(FileAccess.ReadAndWrite);
databaseStream.SetLength(0);
await databaseStream.FlushAsync();
await databaseStream.WriteAsync(bytesToWrite, 0, bytesToWrite.Length);
await databaseStream.FlushAsync();
databaseStream.Dispose();
var cryptoStream = new CryptoRandomStream(CrsAlgorithm.Salsa20, kdb4File.pbProtectedStreamKey,_hasher);
var parser = new Kdb4Parser(cryptoStream);
databaseData.Tree = parser.ParseAndDecode(databaseData.Tree.Document);
}