search
尋找貓咪~QQ 地點 桃園市桃園區 Taoyuan , Taoyuan

C# 二進制讀/寫檔 – jashliao部落格

C# 二進制讀/寫檔




        public static int 二進位寫檔(string 檔名, byte[] 資料)
        {
            try
            {
                //開啟建立檔案
                FileStream file = File.Open(檔名, FileMode.Open, FileAccess.ReadWrite);
                BinaryWriter write = new BinaryWriter(file);
               write.Write(資料);
               write.Close();
                write.Dispose();
                write.Flush();
                file.Close();
                file.Flush();
                file.Dispose();
                return 1;

            }
            catch (InvalidCastException e)
            {
                return -1;
            }
        }


        public static int 二進位讀檔(string 檔名, ref byte[] 資料)
        {
            try
            {
                //開啟檔案
                FileStream file = File.Open(檔名, FileMode.Open, FileAccess.ReadWrite);
                //引用myReader類別
                BinaryReader read = new BinaryReader(file);
                int 長度 = System.Convert.ToInt32(file.Length);
                //讀取位元陣列
                資料 = read.ReadBytes(長度);
                //讀取資料
                //釋放資源
                read.Close();
                read.Flush();
                read.Dispose();
                file.Close();
                file.Flush();
                file.Dispose();
                return 1;
            }
            catch (InvalidCastException e)
            {
                return -1;
            }
        }


 




熱門推薦

本文由 jashliaoeuwordpress 提供 原文連結

寵物協尋 相信 終究能找到回家的路
寫了7763篇文章,獲得2次喜歡
留言回覆
回覆
精彩推薦