程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> C#二進制流的序列化和反序列化,

C#二進制流的序列化和反序列化,

編輯:關於.NET

C#二進制流的序列化和反序列化,


  1  public class BinaryHelper
  2     {
  3         /// <summary>
  4         /// 將對象序列化為byte[]
  5         /// 使用IFormatter的Serialize序列化
  6         /// </summary>
  7         /// <param name="obj">需要序列化的對象</param>
  8         /// <returns>序列化獲取的二進制流</returns>
  9         public static byte[] FormatterObjectBytes(object obj)
 10         {
 11             if(obj==null)
 12                 throw new ArgumentNullException("obj");
 13             byte[] buff;
 14             try
 15             {
 16                 using (var ms = new MemoryStream())
 17                 {
 18                     IFormatter iFormatter = new BinaryFormatter();
 19                     iFormatter.Serialize(ms, obj);
 20                     buff = ms.GetBuffer();
 21                 }
 22             }
 23             catch (Exception er)
 24             {
 25                 throw new Exception(er.Message);
 26             }
 27             return buff;
 28         }
 29 
 30 
 31         /// <summary>
 32         /// 將對象轉為二進制文件,並保存到指定的文件中
 33         /// </summary>
 34         /// <param name="name">文件路徑</param>
 35         /// <param name="obj">待存的對象</param>
 36         /// <returns></returns>
 37         public static bool BinaryFileSave(string name,object obj)
 38         {
 39             Stream flstr=null;
 40             BinaryWriter binaryWriter=null;
 41             try
 42             {
 43                 flstr = new FileStream(name, FileMode.Create);
 44                 binaryWriter = new BinaryWriter(flstr);
 45                 var buff = FormatterObjectBytes(obj);
 46                 binaryWriter.Write(buff);
 47             }
 48             catch (Exception er)
 49             {
 50                 throw new Exception(er.Message);
 51             }
 52             finally
 53             {
 54                 if (binaryWriter != null) binaryWriter.Close();
 55                 if (flstr != null) flstr.Close();
 56             }
 57             return true;
 58         }
 59 
 60         /// <summary>
 61         /// 將byte[]反序列化為對象
 62         /// 使用IFormatter的Deserialize發序列化
 63         /// </summary>
 64         /// <param name="buff">傳入的byte[]</param>
 65         /// <returns></returns>
 66         public static object FormatterByteObject(byte[] buff)
 67         {
 68             if(buff==null)
 69                 throw new ArgumentNullException("buff");
 70             object obj;
 71             try
 72             {
 73                 using (var ms = new MemoryStream())
 74                 {
 75                     IFormatter iFormatter = new BinaryFormatter();
 76                     obj = iFormatter.Deserialize(ms);
 77                 }
 78             }
 79             catch (Exception er)
 80             {
 81                 throw new Exception(er.Message);
 82             }
 83             return obj;
 84         }
 85 
 86 
 87         /// <summary>
 88         /// 將對象序列化為byte[]
 89         /// 使用Marshal的StructureToPtr序列化
 90         /// </summary>
 91         /// <param name="obj">需序列化的對象</param>
 92         /// <returns>序列化後的byte[]</returns>
 93         public static byte[] MarshalObjectByte(object obj)
 94         {
 95             if(obj==null)
 96                 throw new ArgumentNullException("obj");
 97             byte[] buff;
 98             try
 99             {
100                 buff = new byte[Marshal.SizeOf(obj)];
101                 var ptr = Marshal.UnsafeAddrOfPinnedArrayElement(buff, 0);
102                 Marshal.StructureToPtr(obj, ptr, true);
103             }
104             catch (Exception er)
105             {
106                 throw new Exception(er.Message);
107             }
108             return buff;
109         }
110 
111         /// <summary>
112         /// 將byte[]序列化為對象
113         /// </summary>
114         /// <param name="buff">被轉換的二進制流</param>
115         /// <param name="type">轉換成的類名</param>
116         /// <returns></returns>
117         public static object MarshalByteObject(byte[] buff, Type type)
118         {
119             if(buff==null)
120                 throw new ArgumentNullException("buff");
121             if(type==null)
122                 throw new ArgumentNullException("type");
123             try
124             {
125                 var ptr = Marshal.UnsafeAddrOfPinnedArrayElement(buff, 0);
126                 return Marshal.PtrToStructure(ptr, type);
127             }
128             catch (Exception er)
129             {
130                 throw new Exception(er.Message);
131             }
132         }
133 
134 
135         /// <summary>
136         /// 將文件轉換為byte數組
137         /// </summary>
138         /// <param name="path">文件地址</param>
139         /// <returns>轉換後的byte[]</returns>
140         public static byte[] FileObjectBytes(string path)
141         {
142             if(string.IsNullOrEmpty(path))
143                 throw new ArgumentNullException("path");
144             if (!File.Exists(path)) return new byte[0];
145             try
146             {
147                 var fi = new FileInfo(path);
148                 var buff = new byte[fi.Length];
149                 var fs = fi.OpenRead();
150                 fs.Read(buff, 0, Convert.ToInt32(fs.Length));
151                 fs.Close();
152                 return buff;
153             }
154             catch (Exception er)
155             {
156                 throw new Exception(er.Message);
157             }
158         }
159 
160 
161         /// <summary>
162         /// 將byte[]轉換為文件並保存到指定的地址
163         /// </summary>
164         /// <param name="buff">需反序列化的byte[]</param>
165         /// <param name="savePath">文件保存的路徑</param>
166         /// <returns>是否成功</returns>
167         public static string FileByteObject(byte[] buff, string savePath)
168         {
169             if(buff==null)
170                 throw new ArgumentNullException("buff");
171             if(savePath==null)
172                 throw new ArgumentNullException("savePath");
173             if (File.Exists(savePath)) return "文件名重復";
174             try
175             {
176                 var fs = new FileStream(savePath, FileMode.CreateNew);
177                 var bw = new BinaryWriter(fs);
178                 bw.Write(buff, 0, buff.Length);
179                 bw.Close();
180                 fs.Close();
181             }
182             catch (Exception er)
183             {
184                 throw new Exception(er.Message);
185             }
186             return "保存成功";
187         }
188 
189 
190         /// <summary>
191         /// 將圖片序列化為二進制流
192         /// </summary>
193         /// <param name="imgPath">圖片路徑</param>
194         /// <returns>序列化後的二進制流</returns>
195         public static byte[] SetImgToBytes(string imgPath)
196         {
197             if(string.IsNullOrEmpty(imgPath))
198                 throw new ArgumentNullException(imgPath);
199             try
200             {
201                 byte[] byteData;
202                 using (var file=new FileStream(imgPath,FileMode.Open,FileAccess.Read))
203                 {
204                     byteData=new byte[file.Length];
205                     file.Read(byteData, 0, byteData.Length);
206                     file.Close();
207                 }
208                 return byteData;
209             }
210             catch (Exception er)
211             {
212                 
213                 throw new Exception(er.Message);
214             }
215         }
216 
217 
218 
219     }

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved