Sunday, June 30, 2013

III. Using System input/output to get the files and folders in c#



Feeling bored and not motivated, i found myself watching my favorite anime "gintama". While watching I decided to code something just for passing time. I decided to write something about paint and files saving. In game development, saving file is important. You need to save scores, screenshots, equipment, game states , settings etc. There are many ways to read files in your computer. So let's start, create a new c# project , yes your right. We're going to use a console c# application..

Add System.IO in your packages..
using System.IO;

We can now use I/O packages in our console app..
What we wan't to do is to read a specific filepath / location and show all the files inside it..
Declare 2 arrays that will hold the name of folder and files in a directory.
Directory.Get will do the job for us. All we need to do is to assign the collected data into our string array. After that, use loops to write it in the console.


class Program
{
  static void Main(string[] args)
        {
            string[] files = Directory.GetFiles(@"C:\");
            string[] folders= Directory.GetDirectories(@"c:\");
            for (int x = 0; x < folders.Length; x++)
                Console.WriteLine(folders[x]);
            for (int x = 0; x < files.Length; x++)
                Console.WriteLine(files[x]);
            Console.ReadKey();
        }
}

execute the project