|
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
|
Comment : lire du texte dans un fichier
using System; using System.IO; class Test { public static void Main() { try { using (StreamReader sr = new StreamReader("TestFile.txt")) { String line = sr.ReadToEnd(); Console.WriteLine(line); } } catch (Exception e) { Console.WriteLine("The file could not be read:"); Console.WriteLine(e.Message); } } }
using System; using System.Windows; using System.IO; namespace WpfApplication { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private async void ReadFileButton_Click(object sender, RoutedEventArgs e) { try { using (StreamReader sr = new StreamReader("TestFile.txt")) { String line = await sr.ReadToEndAsync(); ResultBlock.Text = line; } } catch (Exception ex) { ResultBlock.Text = "Could not read the file"; } } } }