日期:2014-05-18 浏览次数:22253 次
 public byte[] ReadBytes(string path)
        {
            FileStream fs = null;//= new FileStream(path, FileMode.Open, FileAccess.Read);
            byte[] data=null;
            BinaryReader r = null;// new BinaryReader(fs);
            try
            {
                using (fs = new FileStream(path, FileMode.Open, FileAccess.Read))
                {
                    using (r = new BinaryReader(fs))
                    {
                        data = r.ReadBytes((int)fs.Length);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (fs!=null)
                {
                    fs.Close();
                    fs.Dispose();
                }
                if (r != null)
                {
                    r.Close();
                }
              
            }
            return data;
        }
        public bool PutFile(byte[] buffer, string filepath)
        {
            bool isSuc = false;
            if (File.Exists(filepath))
            {
                File.Delete(filepath);
            }
            BinaryWriter binWriter =null;
            try
            {
                using (binWriter = new BinaryWriter(File.Open(filepath, FileMode.CreateNew, FileAccess.ReadWrite)))
                {
                    binWriter.Write(buffer);
                    binWriter.Flush();
                }
                isSuc = true;
            }
            catch (Exception ex)
            {
                isSuc = false;
                throw ex;
            }
            finally
            {
                if (binWriter!=null)
                {
                    binWriter.Close();
                }
                GC.Collect();
                //GC.Collect(3, GCCollectionMode.Optimized);
            }
            //有此句GC不会回收,无此句GC会回收
            //int length = buffer.Length;
            return isSuc;
        }