Last active
March 17, 2017 04:06
-
-
Save Bamuel/c46c179434b7388cf9cf10b0b3b73483 to your computer and use it in GitHub Desktop.
A template for saving & reading data as .txt in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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