進行寫入動作

using System;
using System.IO;

class Test
{
    public static void Main()
    {
        // Create an instance of StreamWriter to write text to a file.
        // The using statement also closes the StreamWriter.
        using (StreamWriter sw = new StreamWriter("TestFile.TXT"))   //小寫TXT     
        {
            // Add some text to the file.
            sw.Write("This is the ");
            sw.WriteLine("header for the file.");
            sw.WriteLine("-------------------");
            // Arbitrary objects can also be written to the file.
            sw.Write("The date is: ");
            sw.WriteLine(DateTime.Now);
        }
    }
}

讀取動作

using System;
using System.IO;

class Test
{
    public static void Main()
    {
        try { // Create an instance of StreamReader to read from a file.
            // The using statement also closes the StreamReader.
            using (StreamReader sr = new StreamReader("TestFile.TXT"))     //小寫TXT
            {
                String line;
                // Read and display lines from the file until the end of
                // the file is reached.
                while ((line = sr.ReadLine()) != null)
                {
                    Console.WriteLine(line);
                }
            }
        }
        catch (Exception e)
        {
            // Let the user know what went wrong.
            Console.WriteLine("The file could not be read:");
            Console.WriteLine(e.Message);
        }
    }
}

資料來源:http://msdn.microsoft.com/zh-tw/library/db5x7c0d(v=VS.80).aspx

http://blog.xuite.net/autosun/study/32576568 

arrow
arrow
    全站熱搜

    東勢厝滴yang 發表在 痞客邦 留言(1) 人氣()