本文整理汇总了C#中System.IO.FileStream.FlushAsync方法的典型用法代码示例。如果您正苦于以下问题:C# FileStream.FlushAsync方法的具体用法?C# FileStream.FlushAsync怎么用?C# FileStream.FlushAsync使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.FileStream
的用法示例。
在下文中一共展示了FileStream.FlushAsync方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SaveFile
public async Task SaveFile(string name, Stream stream)
{
var fullPath = GetFullPath(name);
using (var fs = new FileStream(fullPath, FileMode.OpenOrCreate, FileAccess.Write))
{
await stream.CopyToAsync(fs).ConfigureAwait(false);
await stream.FlushAsync().ConfigureAwait(false);
await fs.FlushAsync().ConfigureAwait(false);
}
}
示例2: DownloadAsync
public async Task DownloadAsync(string url, IDictionary<string, string> headers, string method, string saveAs)
{
// Download the data
var data = await this.DownloadAsync(url, headers, method);
// Write the file
using (var memStream = new MemoryStream(data))
{
using (var stream = new FileStream(saveAs, FileMode.CreateNew, FileAccess.Write, FileShare.None, (int)1024, true))
{
await memStream.CopyToAsync(stream);
await stream.FlushAsync();
}
}
}
示例3: Create
/// <summary>
/// An async FileLog constructor.
/// </summary>
/// <param name="path">The path to the log file.</param>
/// <param name="useAsync">Instruct the underlying IO subsystem to optimize for async I/O.</param>
/// <returns>An <see cref="FileLog"/> instance.</returns>
public static async Task<FileLog> Create(string path, bool useAsync)
{
if (path == null) throw new ArgumentNullException("path");
path = string.Intern(Path.GetFullPath(path));
var writer = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read, 4096, useAsync);
var buf = new byte[LHDR_SIZE];
// check the file's version number if file exists, else write it out and initialize the log
long next;
Version vers;
if (writer.Length < LHDR_SIZE)
{
next = LHDR_SIZE;
vers = VERSION;
buf.Write(vers.Major, LHDR_MAJOR);
buf.Write(vers.Minor, LHDR_MINOR);
buf.Write(vers.Revision, LHDR_REV);
buf.Write(next, LHDR_TX);
await writer.WriteAsync(buf, 0, LHDR_SIZE);
await writer.FlushAsync();
}
else
{
await writer.ReadAsync(buf, 0, LHDR_SIZE);
var major = buf.ReadInt32(LHDR_MAJOR);
var minor = buf.ReadInt32(LHDR_MINOR);
var rev = buf.ReadInt32(LHDR_REV);
vers = new Version(major, minor, 0, rev);
if (vers != VERSION)
{
writer.Dispose();
throw new NotSupportedException(string.Format("File log expects version {0} but found version {1}", VERSION, vers));
}
next = buf.ReadInt32(LHDR_TX);
}
return new FileLog(path, writer, next);
}
示例4: DeleteAsync
/// <summary>
/// Asynchronously deletes the specified file.
/// </summary>
///
/// <param name="path">The name of the file to be deleted. Wildcard characters are not supported.</param>
/// <remarks>
/// <para>
/// Specify a file name with any relative or absolute path information for the <paramref name="path"/> parameter.
/// Wildcard characters cannot be included. Relative path information is interpreted as relative to the current working directory.
/// To obtain the current working directory, see <see cref="Directory.GetCurrentDirectory"/>.
/// </para>
/// <para>If the file to be deleted does not exist, no exception is thrown.</para>
/// </remarks>
///
/// <exception cref="ArgumentException"><paramref name="path"/> is a zero-length string, contains only white space, or contains one more invalid characters defined by the <see cref="Path.GetInvalidPathChars"/> method.</exception>
/// <exception cref="ArgumentNullException"><paramref name="path"/> is <c>null</c>.</exception>
/// <exception cref="DirectoryNotFoundException">The path specified in <paramref name="path"/> is invalid (for example, it is on an unmapped drive).</exception>
/// <exception cref="IOException">
/// <para>The specified file is in use.</para>
/// <para>-or-</para>
/// <para>There is an open handle on the file, and the operating system is Windows XP or earlier. This open handle can result from enumerating directories and files.</para>
/// </exception>
/// <exception cref="NotSupportedException"><paramref name="path"/> is in an invalid format.</exception>
/// <exception cref="PathTooLongException">
/// The specified path, file name, or both exceed the system-defined maximum length.
/// For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
/// </exception>
/// <exception cref="SecurityException">The caller does not have the required permission.</exception>
/// <exception cref="UnauthorizedAccessException"><paramref name="path"/> specifies a read-only file.
/// <para>-or-</para>
/// <para>The caller does not have the required permission.</para>
/// <para>-or-</para>
/// <para>The file is an executable file that is in use.</para>
/// <para>-or-</para>
/// <para><paramref name="path"/> is a directory.</para>
/// </exception>
///
/// <returns>Task that represents asynchronous operation.</returns>
public static async Task DeleteAsync(string path)
{
PathValidator.EnsureCorrectFileSystemPath(path);
if (File.Exists(path))
{
const int bufferSize = 4096;
using (var fileStream = new FileStream(path, FileMode.Truncate, FileAccess.Write, FileShare.Delete, bufferSize, true))
{
await fileStream.FlushAsync();
File.Delete(path);
}
}
}
示例5: WriteFromStream
public async Task WriteFromStream(Stream sourceStream, FileInfo[] targets)
{
string targetPath = ResolveFileFullPath(targets.First()); // take first - path is based only on hash
// write to files
using (var fileStream = new FileStream(targetPath, FileMode.Create))
using (var gzip = new GZipStream(fileStream, CompressionMode.Compress))
{
await sourceStream.CopyToAsync(gzip);
await fileStream.FlushAsync();
}
// update index
await UpdateFilesList(filesToAddOrUpdate: targets);
}
示例6: DownloadAsync
private async Task DownloadAsync(IExecutionContext executionContext, TaskReference task)
{
Trace.Entering();
ArgUtil.NotNull(executionContext, nameof(executionContext));
ArgUtil.NotNull(task, nameof(task));
ArgUtil.NotNullOrEmpty(task.Version, nameof(task.Version));
var taskServer = HostContext.GetService<ITaskServer>();
// first check to see if we already have the task
string destDirectory = GetDirectory(task);
Trace.Info($"Ensuring task exists: ID '{task.Id}', version '{task.Version}', name '{task.Name}', directory '{destDirectory}'.");
if (Directory.Exists(destDirectory))
{
Trace.Info("Task already downloaded.");
return;
}
Trace.Info("Getting task.");
string zipFile;
var version = new TaskVersion(task.Version);
//download and extract task in a temp folder and rename it on success
string tempDirectory = Path.Combine(IOUtil.GetTasksPath(HostContext), "_temp_" + Guid.NewGuid());
try
{
Directory.CreateDirectory(tempDirectory);
zipFile = Path.Combine(tempDirectory, string.Format("{0}.zip", Guid.NewGuid()));
//open zip stream in async mode
using (FileStream fs = new FileStream(zipFile, FileMode.Create, FileAccess.Write, FileShare.None, bufferSize: 4096, useAsync: true))
{
using (Stream result = await taskServer.GetTaskContentZipAsync(task.Id, version, executionContext.CancellationToken))
{
//81920 is the default used by System.IO.Stream.CopyTo and is under the large object heap threshold (85k).
await result.CopyToAsync(fs, 81920, executionContext.CancellationToken);
await fs.FlushAsync(executionContext.CancellationToken);
}
}
ZipFile.ExtractToDirectory(zipFile, tempDirectory);
File.Delete(zipFile);
Directory.CreateDirectory(Path.GetDirectoryName(destDirectory));
Directory.Move(tempDirectory, destDirectory);
Trace.Info("Finished getting task.");
}
finally
{
try
{
//if the temp folder wasn't moved -> wipe it
if (Directory.Exists(tempDirectory))
{
Trace.Verbose("Deleting task temp folder: {0}", tempDirectory);
IOUtil.DeleteDirectory(tempDirectory, CancellationToken.None); // Don't cancel this cleanup and should be pretty fast.
}
}
catch (Exception ex)
{
//it is not critical if we fail to delete the temp folder
Trace.Warning("Failed to delete temp folder '{0}'. Exception: {1}", tempDirectory, ex);
executionContext.Warning(StringUtil.Loc("FailedDeletingTempDirectory0Message1", tempDirectory, ex.Message));
}
}
}
示例7: SaveUpkFile
public async Task SaveUpkFile(DomainHeader Header, string Filename) {
if (Header == null) return;
foreach(DomainExportTableEntry export in Header.ExportTable.Where(export => export.DomainObject == null)) await export.ParseDomainObject(Header, false, false);
FileStream stream = new FileStream(Filename, FileMode.Create);
int headerSize = Header.GetBuilderSize();
ByteArrayWriter writer = ByteArrayWriter.CreateNew(headerSize);
await Header.WriteBuffer(writer, 0);
await stream.WriteAsync(writer.GetBytes(), 0, headerSize);
foreach(DomainExportTableEntry export in Header.ExportTable) {
ByteArrayWriter objectWriter = await export.WriteObjectBuffer();
await stream.WriteAsync(objectWriter.GetBytes(), 0, objectWriter.Index);
}
await stream.FlushAsync();
stream.Close();
}
示例8: WriteFromStream
public async Task WriteFromStream(Stream sourceStream, FileInfo[] targets)
{
// create copy if multiple targets
if(targets.Length > 1)
{
var copy = new MemoryStream();
await sourceStream.CopyToAsync(copy);
sourceStream = copy;
}
foreach (var target in targets)
{
target.EnsureParentDirectoryExists(Path);
string targetPath = target.ResolveFullPath(Path);
using (var fileStream = new FileStream(targetPath, FileMode.Create))
{
await sourceStream.CopyToAsync(fileStream);
await fileStream.FlushAsync();
}
}
}
示例9: CopyAssets
private async Task<string> CopyAssets ()
{
try {
Android.Content.Res.AssetManager assetManager = _context.Assets;
string[] files = assetManager.List ("tessdata");
File file = _context.GetExternalFilesDir (null);
var tessdata = new File (_context.GetExternalFilesDir (null), "tessdata");
if (!tessdata.Exists ()) {
tessdata.Mkdir ();
} else {
var packageInfo = _context.PackageManager.GetPackageInfo (_context.PackageName, 0);
var version = packageInfo.VersionName;
var versionFile = new File (tessdata, "version");
if (versionFile.Exists ()) {
var fileVersion = System.IO.File.ReadAllText (versionFile.AbsolutePath);
if (version == fileVersion) {
Log.Debug ("[TesseractApi]", "Application version didn't change, skipping copying assets");
return file.AbsolutePath;
}
versionFile.Delete ();
}
System.IO.File.WriteAllText (versionFile.AbsolutePath, version);
}
Log.Debug ("[TesseractApi]", "Copy assets to " + file.AbsolutePath);
foreach (var filename in files) {
using (var inStream = assetManager.Open ("tessdata/" + filename)) {
var outFile = new File (tessdata, filename);
if (outFile.Exists ()) {
outFile.Delete ();
}
using (var outStream = new FileStream (outFile.AbsolutePath, FileMode.Create)) {
await inStream.CopyToAsync (outStream);
await outStream.FlushAsync ();
}
}
}
return file.AbsolutePath;
} catch (Exception ex) {
Log.Error ("[TesseractApi]", ex.Message);
}
return null;
}
示例10: DownloadLatestAgent
/// <summary>
/// _work
/// \_update
/// \bin
/// \externals
/// \run.sh
/// \run.cmd
/// \package.zip //temp download .zip/.tar.gz
/// </summary>
/// <param name="token"></param>
/// <returns></returns>
private async Task<string> DownloadLatestAgent(CancellationToken token)
{
var agentServer = HostContext.GetService<IAgentServer>();
string latestAgentDirectory = IOUtil.GetUpdatePath(HostContext);
IOUtil.DeleteDirectory(latestAgentDirectory, token);
Directory.CreateDirectory(latestAgentDirectory);
string archiveFile = Path.Combine(latestAgentDirectory, $"{new Uri(_latestPackage.DownloadUrl).Segments.Last()}");
try
{
using (var httpClient = new HttpClient())
{
//open zip stream in async mode
using (FileStream fs = new FileStream(archiveFile, FileMode.Create, FileAccess.Write, FileShare.None, bufferSize: 4096, useAsync: true))
{
using (Stream result = await httpClient.GetStreamAsync(_latestPackage.DownloadUrl))
{
//81920 is the default used by System.IO.Stream.CopyTo and is under the large object heap threshold (85k).
await result.CopyToAsync(fs, 81920, token);
await fs.FlushAsync(token);
}
}
}
if (archiveFile.EndsWith(".zip", StringComparison.OrdinalIgnoreCase))
{
ZipFile.ExtractToDirectory(archiveFile, latestAgentDirectory);
}
else if (archiveFile.EndsWith(".tar.gz", StringComparison.OrdinalIgnoreCase))
{
var whichUtil = HostContext.GetService<IWhichUtil>();
string tar = whichUtil.Which("tar");
if (string.IsNullOrEmpty(tar))
{
throw new NotSupportedException($"tar -xzf");
}
// tar -xzf
using (var processInvoker = HostContext.CreateService<IProcessInvoker>())
{
processInvoker.OutputDataReceived += new EventHandler<ProcessDataReceivedEventArgs>((sender, args) =>
{
if (!string.IsNullOrEmpty(args.Data))
{
Trace.Info(args.Data);
}
});
processInvoker.ErrorDataReceived += new EventHandler<ProcessDataReceivedEventArgs>((sender, args) =>
{
if (!string.IsNullOrEmpty(args.Data))
{
Trace.Error(args.Data);
}
});
int exitCode = await processInvoker.ExecuteAsync(latestAgentDirectory, tar, $"-xzf {archiveFile}", null, token);
if (exitCode != 0)
{
throw new NotSupportedException($"Can't use 'tar -xzf' extract archive file: {archiveFile}. return code: {exitCode}.");
}
}
}
else
{
throw new NotSupportedException($"{archiveFile}");
}
Trace.Info($"Finished getting latest agent package at: {latestAgentDirectory}.");
}
finally
{
try
{
// delete .zip file
if (!string.IsNullOrEmpty(archiveFile) && File.Exists(archiveFile))
{
Trace.Verbose("Deleting latest agent package zip: {0}", archiveFile);
IOUtil.DeleteFile(archiveFile);
}
}
catch (Exception ex)
{
//it is not critical if we fail to delete the temp folder
Trace.Warning("Failed to delete agent package zip '{0}'. Exception: {1}", archiveFile, ex);
}
}
return latestAgentDirectory;
//.........这里部分代码省略.........
示例11: GetAsync
internal async Task<HttpSourceResult> GetAsync(string uri, string cacheKey, TimeSpan cacheAgeLimit, bool ignoreNotFounds, CancellationToken cancellationToken)
{
var sw = new Stopwatch();
sw.Start();
var result = await TryCache(uri, cacheKey, cacheAgeLimit);
if (result.Stream != null)
{
Logger.LogVerbose(string.Format(CultureInfo.InvariantCulture, " {0} {1}", "CACHE".Green(), uri));
return result;
}
Logger.LogVerbose(string.Format(CultureInfo.InvariantCulture, " {0} {1}.", "GET".Yellow(), uri));
var request = new HttpRequestMessage(HttpMethod.Get, uri);
if (_userName != null)
{
var token = Convert.ToBase64String(Encoding.UTF8.GetBytes(_userName + ":" + _password));
request.Headers.Authorization = new AuthenticationHeaderValue("Basic", token);
}
;
#if DNXCORE50
if (_proxyUserName != null)
{
var proxyToken = Convert.ToBase64String(Encoding.UTF8.GetBytes(_proxyUserName + ":" + _proxyPassword));
request.Headers.ProxyAuthorization = new AuthenticationHeaderValue("Basic", proxyToken);
}
#endif
var response = await _client.SendAsync(request, cancellationToken);
if (ignoreNotFounds && response.StatusCode == HttpStatusCode.NotFound)
{
Logger.LogInformation(string.Format(CultureInfo.InvariantCulture,
" {1} {0} {2}ms", uri, response.StatusCode.ToString().Green(), sw.ElapsedMilliseconds.ToString().Bold()));
return new HttpSourceResult();
}
response.EnsureSuccessStatusCode();
var newFile = result.CacheFileName + "-new";
// Zero value of TTL means we always download the latest package
// So we write to a temp file instead of cache
if (cacheAgeLimit.Equals(TimeSpan.Zero))
{
result.CacheFileName = Path.GetTempFileName();
newFile = Path.GetTempFileName();
}
// The update of a cached file is divided into two steps:
// 1) Delete the old file. 2) Create a new file with the same name.
// To prevent race condition among multiple processes, here we use a lock to make the update atomic.
await ConcurrencyUtilities.ExecuteWithFileLocked(result.CacheFileName, async _ =>
{
using (var stream = new FileStream(
newFile,
FileMode.Create,
FileAccess.ReadWrite,
FileShare.ReadWrite | FileShare.Delete,
BufferSize,
useAsync: true))
{
await response.Content.CopyToAsync(stream);
await stream.FlushAsync(cancellationToken);
}
if (File.Exists(result.CacheFileName))
{
// Process B can perform deletion on an opened file if the file is opened by process A
// with FileShare.Delete flag. However, the file won't be actually deleted until A close it.
// This special feature can cause race condition, so we never delete an opened file.
if (!IsFileAlreadyOpen(result.CacheFileName))
{
File.Delete(result.CacheFileName);
}
}
// If the destination file doesn't exist, we can safely perform moving operation.
// Otherwise, moving operation will fail.
if (!File.Exists(result.CacheFileName))
{
File.Move(
newFile,
result.CacheFileName);
}
// Even the file deletion operation above succeeds but the file is not actually deleted,
// we can still safely read it because it means that some other process just updated it
// and we don't need to update it with the same content again.
result.Stream = new FileStream(
result.CacheFileName,
FileMode.Open,
FileAccess.Read,
FileShare.Read | FileShare.Delete,
BufferSize,
useAsync: true);
return 0;
});
//.........这里部分代码省略.........