Skip to content

Instantly share code, notes, and snippets.

@Bamuel
Last active March 17, 2017 04:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bamuel/c46c179434b7388cf9cf10b0b3b73483 to your computer and use it in GitHub Desktop.
Save Bamuel/c46c179434b7388cf9cf10b0b3b73483 to your computer and use it in GitHub Desktop.
A template for saving & reading data as .txt in C#
using System;
using System.IO;
namespace ReadData {
class Program {
static void Main(string[] args) {
ReadFile();
}
static void ReadFile() {
StreamReader reader = new StreamReader("MyData.txt");
string record = reader.ReadLine();
string[] strArray = new string[2];
while (record != null) {
strArray = record.Split(',');
string ID = strArray[0];
string Name = strArray[1];
string Message = strArray[2];
Console.WriteLine("ID : " + ID + "\t Name : " + Name + "\t Message : " + Message);
record = reader.ReadLine();
}
reader.Close();
}
}
}
using System;
using System.IO;
namespace SaveData {
class Program {
static void Main(string[] args) {
CreateFile();
}
static void CreateFile() {
StreamWriter writer = new StreamWriter("Data.txt");
writer.WriteLine("The data to be saved into the .txt file");
writer.Close();
Console.WriteLine("File saved succesfully.");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment