java 异步 读写io_.NET(C#)异步文件(Asynchronous File) I/O读写拷贝文件的方法及示代码...

1、异步文件I/O

在 .NET Framework 4 和更早的版本中,您必须使用 BeginRead 和 EndRead 之类的方法来实现异步 I/O 操作。 这些方法仍然在 .NET Framework 4.5 中可用,从而支持传统的代码;但是,异步方法能帮助你更轻松地实现异步 I/O 操作。

C# 和 Visual Basic 分别具有两个用于异步编程的关键字:Async (Visual Basic) 或 async (C#) 修饰符,您可以用来标记包含异步操作的方法。

Await (Visual Basic) 或 await (C#) 运算符,可以应用到异步方法的结果中。

2、进行文件拷贝

使用两个 FileStream 对象把文件从一个目录异步复制到另一个目录。 需要注意 Click 控件的 Button 事件处理程序具有 async 修饰符标记,因为它调用异步方法。using System;

using System.Threading.Tasks;

using System.Windows;

using System.IO;

namespace WpfApplication

{

public partial class MainWindow : Window

{

public MainWindow()

{

InitializeComponent();

}

private async void Button_Click(object sender, RoutedEventArgs e)

{

string StartDirectory = @"c:\Users\exampleuser\start";

string EndDirectory = @"c:\Users\exampleuser\end";

foreach (string filename in Directory.EnumerateFiles(StartDirectory))

{

using (FileStream SourceStream = File.Open(filename, FileMode.Open))

{

using (FileStream DestinationStream = File.Create(EndDirectory + filename.Substring(filename.LastIndexOf('\\'))))

{

await SourceStream.CopyToAsync(DestinationStream);

}

}

}

}

}

}

3、通过StreamReader 和 StreamWriter读写实现文件拷贝private async void Button_Click(object sender, RoutedEventArgs e)

{

string UserDirectory = @"c:\Users\exampleuser\";

using (StreamReader SourceReader = File.OpenText(UserDirectory + "BigFile.txt"))

{

using (StreamWriter DestinationWriter = File.CreateText(UserDirectory + "CopiedFile.txt"))

{

await CopyFilesAsync(SourceReader, DestinationWriter);

}

}

}

public async Task CopyFilesAsync(StreamReader Source, StreamWriter Destination)

{

char[] buffer = new char[0x1000];

int numRead;

while ((numRead = await Source.ReadAsync(buffer, 0, buffer.Length)) != 0)

{

await Destination.WriteAsync(buffer, 0, numRead);

}

}

4、使用StreamReader读取文件

用于在 Windows 8.x 应用商店应用中以 Stream 的形式打开文件的代码隐藏文件和 XAML 文件,并且通过使用 StreamReader 类的实例来读取其内容。 它使用异步方法以流的形式打开文件并读取其内容。using System;

using System.IO;

using System.Text;

using Windows.Storage.Pickers;

using Windows.Storage;

using Windows.UI.Xaml;

using Windows.UI.Xaml.Controls;

namespace ExampleApplication

{

public sealed partial class BlankPage : Page

{

public BlankPage()

{

this.InitializeComponent();

}

private async void Button_Click_1(object sender, RoutedEventArgs e)

{

StringBuilder contents = new StringBuilder();

string nextLine;

int lineCounter = 1;

var openPicker = new FileOpenPicker();

openPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;

openPicker.FileTypeFilter.Add(".txt");

StorageFile selectedFile = await openPicker.PickSingleFileAsync();

using (StreamReader reader = new StreamReader(await selectedFile.OpenStreamForReadAsync()))

{

while ((nextLine = await reader.ReadLineAsync()) != null)

{

contents.AppendFormat("{0}. ", lineCounter);

contents.Append(nextLine);

contents.AppendLine();

lineCounter++;

if (lineCounter > 3)

{

contents.AppendLine("Only first 3 lines shown.");

break;

}

}

}

DisplayContentsBlock.Text = contents.ToString();

}

}

}

XAML文件:

x:Class="ExampleApplication.BlankPage"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:local="using:ExampleApplication"

xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

mc:Ignorable="d">

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值