-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.cs
68 lines (61 loc) · 1.91 KB
/
Utils.cs
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Xml.Serialization;
namespace PEskin
{
public class UBinder : SerializationBinder
{
public override Type BindToType(string assemblyName, string typeName)
{
return (Assembly.GetExecutingAssembly()).GetType(typeName);
}
}
public class Utils
{
public Utils()
{
}
/// <summary>
/// 从指定文件读取设置
/// </summary>
/// <param name="pathFile"></param>
/// <returns></returns>
public static Settings loadSettings(string pathFile)
{
Settings set;
try
{
IFormatter formatter = new BinaryFormatter();
formatter.Binder = new UBinder();
Stream stream = new FileStream(pathFile, FileMode.Open, FileAccess.Read, FileShare.Read);//文件流
set = (Settings)formatter.Deserialize(stream);//反序列化
stream.Close();//关闭流
}
catch (Exception)
{
//读取失败
return new Settings();
}
return set;
}
/// <summary>
/// 保存字体到xml文件
/// </summary>
/// <param name="path"></param>
/// <param name="font"></param>
public static void saveSettings(string path, Settings set)
{
IFormatter Fileformatter = new BinaryFormatter();
Stream Filestream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None);//文件流
Fileformatter.Serialize(Filestream, set);//序列化
Filestream.Close();//关闭流
}
}
}