chr
2024-08-07 22beee93f14d042aa184148c53efb79e23416526
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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();
            }
        }
    }
}