forked from Cinchoo/ChoEazyCopy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChoRoboCopyManager.cs
275 lines (223 loc) · 9.12 KB
/
ChoRoboCopyManager.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
namespace ChoEazyCopy
{
#region NameSpaces
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows;
using Cinchoo.Core.IO;
using Cinchoo.Core;
using System.Threading;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Management;
#endregion NameSpaces
public class ChoFileProcessEventArgs : EventArgs
{
#region Instance Data Members (Public)
public string Message
{
get;
private set;
}
public object Tag
{
get;
private set;
}
#endregion Instance Data Members (Public)
#region Constructors
public ChoFileProcessEventArgs(string message, object tag = null)
{
Message = message;
Tag = tag;
}
#endregion Constructors
}
public class ChoRoboCopyManager : IDisposable
{
#region Shared Data Members (Private)
private static readonly ChoAppSettings _appSettings = new ChoAppSettings();
#endregion Shared Data Members (Private)
#region EventHandlers
public event EventHandler<ChoFileProcessEventArgs> Status;
public event EventHandler<ChoFileProcessEventArgs> AppStatus;
#endregion EventHandlers
#region Instance Data Members (Private)
private Process _process = null;
#endregion Instance Data Members (Private)
#region Constructors
public ChoRoboCopyManager(string settingsFilePath = null)
{
if (settingsFilePath.IsNullOrWhiteSpace())
{
if (File.Exists(settingsFilePath))
{
string settingsText = File.ReadAllText(settingsFilePath);
_appSettings.LoadXml(settingsText);
}
}
}
#endregion Constructors
#region Instance Members (Public)
public void Process(string fileName, string arguments, ChoAppSettings appSettings, bool console = false)
{
AppStatus.Raise(this, new ChoFileProcessEventArgs("Starting RoboCopy operation..."));
Status.Raise(this, new ChoFileProcessEventArgs(Environment.NewLine));
string preCommands = appSettings.Precommands;
string postCommands = appSettings.Postcommands;
try
{
// Setup the process start info
var processStartInfo = new ProcessStartInfo("cmd.exe", " /K /E:OFF /F:OFF /V:OFF") // new ProcessStartInfo(fileName, arguments) //_appSettings.RoboCopyFilePath, _appSettings.GetCmdLineParams(sourceDirectory, destDirectory))
{
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
};
// Setup the process
Process process = new Process { StartInfo = processStartInfo, EnableRaisingEvents = true };
// Register event
_process = process;
// Start process
process.Start();
//process.BeginOutputReadLine();
Task.Factory.StartNew(new Action<object>(ReadFromStreamReader), process.StandardOutput);
//Task.Factory.StartNew(new Action<object>(ReadFromStreamReader), process.StandardError);
if (!console)
process.StandardInput.WriteLine("prompt $G");
//Run precommands
if (!preCommands.IsNullOrWhiteSpace())
{
//Replace tokens
preCommands = preCommands.Replace("{{SRC_DIR}}", appSettings.SourceDirectory);
preCommands = preCommands.Replace("{{DEST_DIR}}", appSettings.DestDirectory);
foreach (var cmd in preCommands.SplitNTrim().Select(c => c.NTrim()).Select(c => MarshalCmd(c, appSettings)).Where(c => !c.IsNullOrWhiteSpace()))
process.StandardInput.WriteLine($"{cmd}");
}
process.StandardInput.WriteLine($"{fileName} {arguments}");
//Run postcommands
if (!postCommands.IsNullOrWhiteSpace())
{
//Replace tokens
postCommands = postCommands.Replace("{{SRC_DIR}}", appSettings.SourceDirectory);
postCommands = postCommands.Replace("{{DEST_DIR}}", appSettings.DestDirectory);
foreach (var cmd in postCommands.SplitNTrim().Select(c => c.NTrim()).Select(c => MarshalCmd(c, appSettings)).Where(c => !c.IsNullOrWhiteSpace()))
process.StandardInput.WriteLine($"{cmd}");
}
process.StandardInput.WriteLine("exit");
process.WaitForExit();
_process = null;
AppStatus.Raise(this, new ChoFileProcessEventArgs("RoboCopy operation completed successfully.", "RoboCopy operation completed successfully"));
}
catch (ThreadAbortException)
{
Status.Raise(this, new ChoFileProcessEventArgs(Environment.NewLine + "RoboCopy operation canceled by user." + Environment.NewLine, "RoboCopy operation failed."));
AppStatus.Raise(this, new ChoFileProcessEventArgs("RoboCopy operation canceled by user.", "RoboCopy operation failed."));
}
catch (Exception ex)
{
Status.Raise(this, new ChoFileProcessEventArgs(Environment.NewLine + ex.ToString() + Environment.NewLine));
AppStatus.Raise(this, new ChoFileProcessEventArgs("RoboCopy operation failed.", "RoboCopy operation failed."));
}
}
private string MarshalCmd(string cmd, ChoAppSettings appSettings)
{
if (cmd != null)
{
cmd = cmd.Replace(@"{SRC_DIR}", appSettings.SourceDirectory);
cmd = cmd.Replace(@"{DEST_DIR}", appSettings.DestDirectory);
}
return cmd;
}
bool cleanup = false;
private string CleanUp(string txt)
{
//if (!cleanup)
//{
// if (txt.Contains(Environment.NewLine))
// txt = txt.Substring(txt.IndexOf(Environment.NewLine));
// else
// txt = null;
// cleanup = true;
//}
return txt;
}
void ReadFromStreamReader(object state)
{
cleanup = false;
StreamReader reader = state as StreamReader;
char[] buffer = new char[32768];
int chars;
StringBuilder txt = new StringBuilder();
while ((chars = reader.Read(buffer, 0, buffer.Length)) > 0)
{
string data = new string(buffer, 0, chars);
txt.Append(data);
if (txt.Length > 0)
{
Status.Raise(this, new ChoFileProcessEventArgs(CleanUp(txt.ToString())));
txt.Clear();
}
}
if (txt.Length > 0)
{
Status.Raise(this, new ChoFileProcessEventArgs(CleanUp(txt.ToString())));
txt.Clear();
}
// You arrive here when process is terminated.
}
internal void Cancel()
{
Process process = _process;
if (process == null) return;
try
{
try
{
KillProcessAndChildrens(_process.Id);
}
catch { }
process.Kill();
AppStatus.Raise(this, new ChoFileProcessEventArgs("RoboCopy operation canceled."));
_process = null;
}
catch { }
}
private void KillProcessAndChildrens(int pid)
{
ManagementObjectSearcher processSearcher = new ManagementObjectSearcher
("Select * From Win32_Process Where ParentProcessID=" + pid);
ManagementObjectCollection processCollection = processSearcher.Get();
// We must kill child processes first!
if (processCollection != null)
{
foreach (ManagementObject mo in processCollection)
{
KillProcessAndChildrens(Convert.ToInt32(mo["ProcessID"])); //kill child processes(also kills childrens of childrens etc.)
}
}
// Then kill parents.
try
{
Process proc = System.Diagnostics.Process.GetProcessById(pid);
if (!proc.HasExited) proc.Kill();
}
catch (ArgumentException)
{
// Process already exited.
}
}
#endregion Instance Members (Public)
#region IDisposable Members
public void Dispose()
{
}
#endregion
}
}