日期:2014-05-19  浏览次数:21613 次

用FileStream读写文件时候需要用Close()方法关闭吗?
using   System;
using   System.Collections.Generic;
using   System.Text;
using   System.IO;

namespace   WriteFile
{
        class   Program
        {
                static   void   Main(string[]   args)
                {
                        byte[]   byData;
                        char[]   charData;
                        try
                        {
                                FileStream   aFile   =   new   FileStream( "Temp.txt ",   FileMode.Create);
                                charData   =   "My   pink   half   of   the   drainpipe. ".ToCharArray();
                                byData   =   new   byte[charData.Length];

                                Encoder   e   =   Encoding.UTF8.GetEncoder();
                                e.GetBytes(charData,   0,   charData.Length,   byData,   0,   true);

                                aFile.Seek(0,   SeekOrigin.Begin);
                                aFile.Write(byData,   0,   byData.Length);
                        }
                        catch   (IOException   ex)
                        {
                                Console.WriteLine(ex.Message);
                                Console.ReadKey();
                                return;
                        }
                }
        }
}
这段是书里的原代码,它没有用Close请问我需要添加个Close()吗?

------解决方案--------------------
这个不用关闭,因为aFile.Write(byData, 0, byData.Length)之后程序会结束运行.