Reading Data
I/O stands for Input / Output. In broader sense, I/O refers to mechanism of communication between two points. One points sends output and another receives the input. Ex: keyboard sends the input to be an displayed output for monitor. In C#, we type input at the prompt and it shows an output at that time.
We'll be using streams to transport our data. Stream is like a flow of our data. We'll create football games to represent this chapter.
Let's create our project on Visual Studio and integrate it with GitHub. Then, do following steps:
- Right-click at folder in solution explorer > Add > new Item > General > Text Files > named with data.txt
- Right-click on data.txt > Properties > Copy to ouput directory > copy if newer
Let's get in code.
We are working with directory in our folder, first thing we need is show the current directory.
class Program
{
static void Main(string[] args)
{
string currentDirectory = Directory.GetCurrentDirectory();
Console.WriteLine(currentDirectory);
}
}
Now, we want to get the info of our current directory. So, type this code.
DirectoryInfo directory = new DirectoryInfo(currentDirectory);
Then, we want to iterate all files in our current folder.
class Program
{
static void Main(string[] args)
{
string currentDirectory = Directory.GetCurrentDirectory();
DirectoryInfo directory = new DirectoryInfo(currentDirectory);
var files = directory.GetFiles();
foreach (var file in files)
{
Console.WriteLine(file.Name);
}
}
}
If we want to be specific, we could type:
var files = directory.GetFiles("*.txt");