1. 程式人生 > >資料byte形式備份儲存到本地檔案,反序列化讀取出資料恢復

資料byte形式備份儲存到本地檔案,反序列化讀取出資料恢復

前提:

需要備份的資料由介面 object轉換成byte[],備份儲存到本地檔案;

選擇本地檔案,取得資料 byte[]轉換成object,用於資料恢復。

步驟如下:

1.把物件(資料)list ( object ) 序列化並返回相應的位元組byte[]

public byte[] SerializeObject(object pObj)
        {
            byte[] listRead = null;
            try
            {
                if (pObj == null)
                    return null;
                System.IO.MemoryStream _memory = new System.IO.MemoryStream();
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(_memory, pObj);
                _memory.Position = 0;
                byte[] read = new byte[_memory.Length];
                _memory.Read(read, 0, read.Length);
                _memory.Close();
                byte[] sdata = new byte[read.Length];
                Buffer.BlockCopy(read, 0, sdata, 0, read.Length);
                listRead = sdata;
                return listRead;
            }
            catch /*(Exception ex)*/
            {
                return listRead;
            }
        }

2.備份,將資料儲存到本地“文件”目錄,.odr字尾,byte[]位元組格式

public bool DataInfoBackUp(object listTobeBack, string backType)
        {
            if(listTobeBack == null || backType == null)
            {
                MainWindow.m_log.Error("FileDBrowser.xaml > DataInfoBackUp : param is null!");
                return false;
            }

            Microsoft.Win32.SaveFileDialog save= new Microsoft.Win32.SaveFileDialog();
            save.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

            // 設定檔案型別 
            save.Title = App.GetString("ID_WK_104");                             //標題"另存為"
            save.Filter = "(*.odr)|*.odr";
            save.FileName = "[" + backType + "]" +                              // 型別
                            "[" + System.DateTime.Now.ToString("yyyyMMdd_HHmmss") + "]";  // 儲存日期_儲存時間

            // 設定預設檔案型別顯示順序 
            save.FilterIndex = 1;
            // 儲存對話方塊是否記憶上次開啟的目錄 
            save.RestoreDirectory = true;
            // 點了儲存按鈕進入 
            if (save.ShowDialog() == true)
            {
                string file = save.FileName;
                //為檔案開啟一個二進位制寫入器
                FileStream fs;
                fs = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Write);
                BinaryWriter bw = new BinaryWriter(fs);

                byte[] listByteArray = m_mainWin.SerializeObject(listTobeBack);
                bw.Write(listByteArray);

                fs.Close();
                bw.Close();
            }
            return true;
        }

3.恢復,讀取出本地檔案的資料byte[],轉換成物件object     (ToBeDone:轉換成對應型別的資料,更新到DB、介面...)

public void GetOrdersInfoFromFile(ref object outobj)
        {
            Microsoft.Win32.OpenFileDialog open = new Microsoft.Win32.OpenFileDialog();
            open.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            open.Title = App.GetString("ID_WK_105");
            open.Filter = "(*.odr)|*.odr";
            open.FilterIndex = 1;
            // 儲存對話方塊是否記憶上次開啟的目錄 
            open.RestoreDirectory = true;

            if (open.ShowDialog() == true)
            {
                string file = open.FileName;
                FileStream fs;
                fs = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Read);
                BinaryReader br = new BinaryReader(fs);
                byte[] listorder = br.ReadBytes((int)fs.Length);
                outobj = m_mainWin.DeserializeObject(listorder);
                fs.Close();
                br.Close();
            }
        }

4.位元組byte[]資料反序列化成相應的物件object

        public object DeserializeObject(byte[] pBytes)
        {
            object _newOjb = null;
            if (pBytes == null || pBytes.Count() == 0)
            {
                return _newOjb;
            }
            try
            {
                byte[] bytes = pBytes.Cast<byte>().ToArray();
                System.IO.MemoryStream _memory = new System.IO.MemoryStream(bytes);
                _memory.Position = 0;
                BinaryFormatter formatter = new BinaryFormatter();
                _newOjb = formatter.Deserialize(_memory);
                _memory.Close();
            }
            catch /*(Exception ex)*/
            {

            }
            return _newOjb;
        }