在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. javascript关于闭包变量作用域

    在项目中不时会遇到的一些小的问题以及解决办法: 1子函数调用父函数中的变量: 加return: var a=1; function num(){ var b=2; return b; } num()+ ...

  2. information_schema系列二(列,列权限,事件,存储引擎)

    这个系列的文章主要是为了能够让自己了解MySQL5.7的一些系统表,统一做一下备注和使用,也希望分享出来让大家能够有一点点的受益. 1:COLUMNS 老规矩.查一下这个表,看一下记录,由于这个是看表 ...

  3. Scala class的构造方法与继承

    有java背景的人,很清楚java是如何定义构造方法以及继承的.在scala里面,继承和java有些相似.但是构造方法的定义,就不大一样了,应该说是差别还是很大的.在java里面,定义构造方法,就是定 ...

  4. [纯小白学习OpenCV系列]官方例程01:Load and Display an Image

    Version: OpenCV 2.4.9 IDE    : VS2010 OS     : Windows --------------------------------------------- ...

  5. 退役?OR 继续

    今天突然想了好多.上次CB那么决绝的退役,还有其他的一些东西.觉得大家说的都挺对的.看待问题的方式不同,然后或多或少就变了.预备役的事情就是想能不能不让大家因为上不了场退役. 想起以前的自己那么坚决的 ...

  6. IDE警告信息不应该被忽略

    当ide进行编译时提示有变量未使用 可能意味着变量使用错误 因为我们没理由创建一个变量却不去使用他 如if语句判断一个int 或者日期 等其他非空类型是否为null 总是true 这意味这我们在逻辑处 ...

  7. NVelocity 表格行奇偶样式变换

    #foreach($test in $tests) #even <tr class="Test1"> #odd <tr class="Test2&quo ...

  8. 反射类属性生成DataTable

    public class People //类名 { private static string name; //字段 private string sex;//字段 public string Se ...

  9. JavaScript 中2个等号与3个等号的区别

    首先,== equality 等同,=== identity 恒等. ==, 两边值类型不同的时候,要先进行类型转换,再比较. ===,不做类型转换,类型不同的一定不等. 下面分别说明: 先说 === ...

  10. [Python] 关于64位机的numpy安装问题

    最近刚换成64位的系统,重新安装了win10,VS也从原来的2010变为了现在的2013. 利用原来32位电脑硬盘里的python2.7安装包安装,然后打算安装numpy. 上来碰到问题:在windo ...