using Newtonsoft.Json.Linq; using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; namespace PdmSwPlugin.Common.Util { public class FileUtil { public static byte[] toByteArray(string path) { byte[] res = null; using (FileStream fsRead = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { BinaryReader r = new BinaryReader(fsRead); r.BaseStream.Seek(0, SeekOrigin.Begin); res = r.ReadBytes((int)r.BaseStream.Length); } return res; } public static byte[] ObjectTpByteArray(object obj) { JContainer json; try { json = JObject.FromObject(obj); } catch { json = JArray.FromObject(obj); } using (MemoryStream ms = new MemoryStream()) { IFormatter f = new BinaryFormatter(); f.Serialize(ms, json); return ms.GetBuffer(); } } } }