CSharp - Files

From PeformIQ Upgrade
Jump to navigation Jump to search

Notes

Also see - http://msdn.microsoft.com/en-us/library/system.io.file.aspx

using System;
using System.IO;

class Test
{
    public static void Main()
    {
        string path = @"c:\temp\Test.txt";
        if (!File.Exists(path))
        {
            // Create a file to write to. 
            using (StreamWriter sw = File.CreateText(path))
            {
                sw.WriteLine("Hello World");
            }
        }

        // Open the file to read from. 
        using (StreamReader sr = File.OpenText(path))
        {
            string s = "";
            while ((s = sr.ReadLine()) != null)
            {
                Console.WriteLine(s);
            }
        }
    }
}