|
Cet article a fait l'objet d'une traduction automatique. Déplacez votre pointeur sur les phrases de l'article pour voir la version originale de ce texte. Informations supplémentaires.
|
Traduction
Source
|
E/S sur fichier asynchrones
-
Le modificateur Async (Visual Basic) ou async (c#), qui est utilisé pour marquer une méthode qui contient une opération asynchrone. -
L'opérateur Await (Visual Basic) ou await (C#), qui est appliqué au résultat d'une méthode 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); } } } } } }
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); } }
Imports System.Text Imports System.IO Imports Windows.Storage.Pickers Imports Windows.Storage NotInheritable Public Class BlankPage Inherits Page Private Async Sub Button_Click_1(sender As Object, e As RoutedEventArgs) Dim contents As StringBuilder = New StringBuilder() Dim nextLine As String Dim lineCounter As Integer = 1 Dim openPicker = New FileOpenPicker() openPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary openPicker.FileTypeFilter.Add(".txt") Dim selectedFile As StorageFile = Await openPicker.PickSingleFileAsync() Using reader As StreamReader = New StreamReader(Await selectedFile.OpenStreamForReadAsync()) nextLine = Await reader.ReadLineAsync() While (nextLine <> Nothing) contents.AppendFormat("{0}. ", lineCounter) contents.Append(nextLine) contents.AppendLine() lineCounter = lineCounter + 1 If (lineCounter > 3) Then contents.AppendLine("Only first 3 lines shown.") Exit While End If nextLine = Await reader.ReadLineAsync() End While End Using DisplayContentsBlock.Text = contents.ToString() End Sub End Class
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(); } } }
<Page 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"> <StackPanel Background="{StaticResource ApplicationPageBackgroundBrush}" VerticalAlignment="Center" HorizontalAlignment="Center"> <TextBlock Text="Display lines from a file."></TextBlock> <Button Content="Load File" Click="Button_Click_1"></Button> <TextBlock Name="DisplayContentsBlock"></TextBlock> </StackPanel> </Page>