HOW TO GET DATA FROM A TXT FILE IN C# code example

Example 1: save text input into a txt file in c#

using System;  
using System.Collections.Generic;  
using System.ComponentModel;  
using System.Data;  
using System.Drawing;  
using System.Linq;  
using System.Text;  
using System.Windows.Forms;  
using System.IO;  
  
namespace WindowsFormsApplication2  
{  
    public partial class Form1: Form  
    {  
        public Form1()  
        {  
            InitializeComponent();  
        }  
  
        private void button1_Click(object sender, EventArgs e)  
        {  
            TextWriter txt = new StreamWriter("C:\\demo\\demo.txt");  
            txt.Write(textBox1.Text);  
            txt.Close();  
  
        }  
    }  
}

Example 2: load information with txt file to uwp c#

private async Task AddTextToFile(String textToSave)
{
    var appFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
    var file = await appFolder.CreateFileAsync("exposure.txt", 
        Windows.Storage.CreationCollisionOption.OpenIfExists);
    await Windows.Storage.FileIO.AppendTextAsync(file, textToSave + Environment.NewLine);
    // Look in Output Window of Visual Studio for path to file
    System.Diagnostics.Debug.WriteLine(String.Format("File is located at {0}", file.Path.ToString()));
}

private async void Button_Click(object sender, RoutedEventArgs e)
{
    await AddTextToFile(String.Format("MinimumExposure {0}", DateTime.Now.Millisecond.ToString()));
}