-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorkspaceManager.cs
143 lines (126 loc) · 5.15 KB
/
WorkspaceManager.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using System;
using System.IO;
using System.Text;
using System.Globalization;
using System.Xml.Serialization;
using System.Xml;
namespace AutoUpdate {
/// <summary>
/// 管理工作区,例如新增、保存等
/// </summary>
public class WorkspaceManager {
public static WorkspaceManager GetManager() {
var ser = _default.CreateSerializer(typeof(WorkspaceManager));
string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"AutoUpdate\user.xml");
if (File.Exists(path)) {
try {
var xml = File.ReadAllText(path);
using (StringReader reader = new StringReader(xml)) {
using (XmlTextReader xmlReader = new XmlTextReader(reader)) {
return (WorkspaceManager)ser.Deserialize(xmlReader);
}
}
}
catch {
return new WorkspaceManager();
}
}
else {
return new WorkspaceManager();
}
}
public void Save() {
var ser = _default.CreateSerializer(typeof(WorkspaceManager));
string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"AutoUpdate\");
try {
if (!Directory.Exists(path)) {
Directory.CreateDirectory(path);
}
path = Path.Combine(path, "user.xml");
var sb = new StringBuilder(512);
using (var xmlWriter = XmlWriter.Create(sb)) {
ser.Serialize(xmlWriter, this);
File.WriteAllText(path, sb.ToString(), Encoding.UTF8);
}
}
catch {
}
}
private static XmlSerializerFactory _default = new XmlSerializerFactory();
public virtual string GetText(Workspace item) {
var ser = _default.CreateSerializer(typeof(Workspace));
var sb = new StringBuilder(512);
using (var xmlWriter = XmlWriter.Create(sb)) {
ser.Serialize(xmlWriter, item);
return sb.ToString();
}
}
public virtual Workspace GetWorkspace(string xml) {
var ser = _default.CreateSerializer(typeof(Workspace));
using (StringReader reader = new StringReader(xml)) {
using (XmlTextReader xmlReader = new XmlTextReader(reader)) {
return (Workspace)ser.Deserialize(xmlReader);
}
}
}
private PathHistoryCollection _history;
public PathHistoryCollection History {
get {
if (_history == null) {
_history = new PathHistoryCollection();
}
return _history;
}
}
public Workspace Open(string path) {
try {
string fileName = Path.Combine(path, "AutoUpdate.xml");
Workspace workspace;
if (File.Exists(fileName)) {
var xml = File.ReadAllText(fileName, Encoding.UTF8);
workspace = GetWorkspace(xml);
workspace.TargetPath = path;
this.History.Add(path);
}
else {
workspace = new Workspace() {
OnlyLastHour = true,
TargetPath = path
};
}
workspace.Dirty = false;
return workspace;
}
catch (Exception ex) {
throw new ApplicationException(string.Format(CultureInfo.CurrentUICulture, "打开路径 {0} 下的文件失败。", path), ex);
}
}
public void Save(Workspace workspace) {
try {
string fileName = Path.Combine(workspace.TargetPath, "AutoUpdate.xml");
File.WriteAllText(fileName, GetText(workspace), Encoding.UTF8);
workspace.Dirty = false;
this.History.Add(workspace.TargetPath);
}
catch (Exception ex) {
throw new ApplicationException(string.Format(CultureInfo.CurrentUICulture, "保存到路径 {0} 下失败。", workspace.TargetPath), ex);
}
}
public Workspace SaveAs(Workspace workspace, string newPath) {
try {
string fileName = Path.Combine(newPath, "AutoUpdate.xml");
//clone
var xml = GetText(workspace);
var newWorkspace = GetWorkspace(xml);
newWorkspace.TargetPath = newPath;
File.WriteAllText(fileName, GetText(newWorkspace), Encoding.UTF8);
newWorkspace.Dirty = false;
this.History.Add(newPath);
return newWorkspace;
}
catch (Exception ex) {
throw new ApplicationException(string.Format(CultureInfo.CurrentUICulture, "另存到路径 {0} 下失败。", newPath), ex);
}
}
}
}