在C#/.NET中,将文本内容写入文件最简单的方法是调用 File.WriteAllText() 方法,但这个方法没有异步的实现,要想用异步,只能改用有些复杂的 FileStream.WriteAsync() 方法。

使用 FileStream.WriteAsync() 有2个需要注意的地方,1是要设置bufferSize,2是要将useAsync这个构造函数参数设置为true,示例调用代码如下:

public async Task CommitAsync()
{
var bits = Encoding.UTF8.GetBytes("{\"text\": \"test\"}");
using (var fs = new FileStream(
path: @"C:\temp\test.json",
mode: FileMode.Create,
access: FileAccess.Write,
share: FileShare.None,
bufferSize: ,
useAsync: true))
{
await fs.WriteAsync(bits, , bits.Length);
}
}

看这个方法的帮助文档中对useAsync参数的说明:

//   useAsync:
// Specifies whether to use asynchronous I/O or synchronous I/O. However, note
// that the underlying operating system might not support asynchronous I/O,
// so when specifying true, the handle might be opened synchronously depending
// on the platform. When opened asynchronously, the System.IO.FileStream.BeginRead(System.Byte[],System.Int32,System.Int32,System.AsyncCallback,System.Object)
// and System.IO.FileStream.BeginWrite(System.Byte[],System.Int32,System.Int32,System.AsyncCallback,System.Object)
// methods perform better on large reads or writes, but they might be much slower
// for small reads or writes. If the application is designed to take advantage
// of asynchronous I/O, set the useAsync parameter to true. Using asynchronous
// I/O correctly can speed up applications by as much as a factor of 10, but
// using it without redesigning the application for asynchronous I/O can decrease
// performance by as much as a factor of 10.

从中可以得知,只有设置useAsync为true,才真正使用上了异步IO。

C#异步将文本内容写入文件的更多相关文章

  1. java 将内容写入文件 txt

    @Test //将内容写入文件 public void xieru() throws Exception{ FileWriter fileWriter=new FileWriter("d:\ ...

  2. JAVA-将内容写入文件并导出到压缩包

    取出数据库表中的内容写入到文件,并将所有文件写入到压缩包最终导出到指定的某目录下        //导出的压缩包格式  xxxx_date        Date currentTime = new ...

  3. C# 读取txt文本内容写入到excel

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  4. IOS应用发布NSLog的注释及使用重定向,把控制台内容写入文件

    1 选择工程的Target -> Build Settings -> Preprocessor Macros. 如图,默认 Debug项,是“DEBUG=1”. 2 在程序中设置全局宏定义 ...

  5. c#读取并异步写入文件,简单版,指定编码,保持原格式。

    1.同步读取和写入 StreamReader objReader = new StreamReader("E://workspace//zzz//read.txt", Encodi ...

  6. C#读取和写入文件

    一.读取文件 如果你要读取的文件内容不是很多, 可以使用 File.ReadAllText(FilePath) 或指定编码方式 File.ReadAllText(FilePath, Encoding) ...

  7. 【php性能优化】关于写入文件操作的取舍方案

    对于使用php对文件进行写入操作有两种方案一种使用 file_put_contents() 和 fopen()/fwrite()/fclose() 两种方案至于应该怎么选,我觉得应该分情况选择,下面是 ...

  8. NIO学习:使用Channel、Buffer写入文件

    NIO的效率要高于标准IO,因为NIO将最耗时的IO操作(填充和提取缓冲区)转移会操作系统.NIO以块为单位传输数据,相比标准IO的以字节为单位效率要高很多. 通道和缓冲时NIO的核心对象,每个NIO ...

  9. PrintWriter 和 BufferedWriter 写入文件.

    Ref: should I use PrintWriter to wrap BufferedWriter? The main reason for using PrintWriter is the w ...

随机推荐

  1. linux基础知识(四)

    •查看ip地址,ifconfig命令 •重启.启动.停止网络命令 •service network restart/start/stop   •VMnet0:用于虚拟桥接网络下的虚拟交换机 •VMne ...

  2. Nginx 学习

    1.Nginx编译安装 nginx依赖于pcre库,需要先安装pcre(二进制包,跟正则表达式有关),pcre-devel(头文件) configure  --prefix=/usr/local/ng ...

  3. 三星framebuffer驱动代码分析

    一.驱动总体概述 本次的驱动代码是Samsung公司为s5pv210这款SoC编写的framebuffer驱动,对应于s5pv210中的内部外设Display Controller (FIMD)模块. ...

  4. Java设计模式——适配器模式

    JAVA 设计模式 适配器模式 用途 适配器模式 (Adapter) 将一个类的接口转换成客户希望的另外一个接口. Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作. 适配器 ...

  5. 构建ASP.NET网站十大必备工具(1)

    最近使用ASP.NET为公司构建了一个简单的公共网站(该网站的地址:http://superexpert.com/).在这个过程中,我们使用了数量很多的免费工具,如果把构建ASP.NET网站的必备工具 ...

  6. Visio控件关闭“形状”面板

    Visio.Window winShapeSearch = axDrawingControl1.Window.Windows.get_ItemFromID((int)Visio.VisWinTypes ...

  7. ffffffuzzzzzzzzzzzzing

    为了克服现有 Fuzzing 技术对目标程序缺乏理解.测试完全随机且盲目的缺点,提出了新方法:基于动态符号执行的智能测试技术. 先通过静态分析获得危险点,然后通过符号执行收集路径条件,最后利用约束求解 ...

  8. sp_MSforeachtable使用方法

    1)说明系统存储过程sp_MSforeachtable和sp_MSforeachdb,是微软提供的两个不公开的存储过程,从ms sql 6.5开始.存放在SQL Server的MASTER数据库中. ...

  9. iOS9支付完成无法获取回调

    - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id ...

  10. java 抽象类与接口的区别

    在Java 语言中, abstract class 和interface 是支持抽象类 定义的两种机制.正是由于这两种机制的存在,才赋予 了Java强大的 面向对象能力.abstract class和 ...