Repository navigation
Expand file tree
/
Copy pathTaskExplorer.cs
More file actions
278 lines (245 loc) · 10.6 KB
/
Copy pathTaskExplorer.cs
File metadata and controls
278 lines (245 loc) · 10.6 KB
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
276
277
278
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.IO;
using System.Threading.Tasks;
using System.Data;
using Microsoft.Data.SqlClient;
using Dapper;
using FileExplorerMultiTask.Models;
using FileExplorerMultiTask.DataLayer;
using FileExplorerMultiTask.Services;
namespace FileExplorerMultiTask
{
public class TaskExplorer
{
public long CreationTime;
public string Name;
public int ThreadNum;
public long NIterations;
private EntityList<ExplorerObjects> cacheListFiles { get; set; }
private EntityList<ExplorerObjects> cacheListDirs { get; set; }
public delegate long RunTask(string action, string parms);
private bool _bStopped { get; set; }
private Logging _log { get; }
private Logging.WriteMessage _logMessage { get; }
private Logging.WriteMessageToDbLog _logMessageDbLog { get; }
private IActionDBExplorer<ExplorerTasks> ActionExTask;
private IActionDBExplorer<ExplorerObjects> ActionExObj;
public TaskExplorer(Logging log)
{
_log = log;
_logMessage = new Logging.WriteMessage(_log.ToPrint);
_logMessageDbLog = new Logging.WriteMessageToDbLog(_log.ToDbLog);
ActionExTask = new ActionDBExplorer<ExplorerTasks>(_log);
ActionExObj = new ActionDBExplorer<ExplorerObjects>(_log);
}
private bool InsertTask(string Message)
{
try
{
_bStopped = false;
var dp = new DynamicParameters();
Guid Keyid = Guid.NewGuid();
dp.Add("@Keyid", Keyid, DbType.Guid, ParameterDirection.Input);
dp.Add("@Name", Name, DbType.AnsiString, ParameterDirection.Input);
dp.Add("@Message", Message, DbType.AnsiString, ParameterDirection.Input);
dp.Add("@ThreadNum", ThreadNum, DbType.Int32, ParameterDirection.Input);
dp.Add("@NIterations", NIterations, DbType.Int32, ParameterDirection.Input);
dp.Add("@Actived", 1, DbType.Int32, ParameterDirection.Input);
int Id = ActionExTask.Insert(dp);
if (Keyid.ToString().Length > 0)
_log.Keyid = Keyid;
_logMessage(this.ToString(), 0, " - Insert Task: " + Message);
_logMessageDbLog(this.ToString(), 0, "Insert Task in Db: " + Message);
return true;
}
catch (Exception ex)
{
_logMessage(this.ToString(), ex.HResult, ex.Message);
_logMessageDbLog(this.ToString(), ex.HResult, ex.Message);
}
return false;
}
private bool FinishTask(string Message)
{
bool result = false;
try
{
_logMessage(this.ToString(), 0, string.Format("Closed Task {0} in Db: [{1}]", Name, Message));
_logMessageDbLog(this.ToString(), 0, string.Format("Closed Task {0} in Db: [{1}]", Name, Message));
var dp = new DynamicParameters();
dp.Add("@Keyid", _log.Keyid, DbType.Guid, ParameterDirection.Input);
ActionExTask.Update(dp);
result = true;
}
catch (Exception ex)
{
_logMessage(this.ToString(), ex.HResult, ex.Message);
_logMessageDbLog(this.ToString(), ex.HResult, ex.Message);
}
finally
{
_bStopped = true;
}
return result;
}
private long Cleaning()
{
long result = 0;
try
{
_logMessage(this.ToString(), 0, "Inizio pulizia archivi!");
_logMessageDbLog(this.ToString(), 0, "Inizio pulizia archivi!");
var dp = new DynamicParameters();
dp.Add("@id", 0, DbType.Int32, ParameterDirection.Input);
ActionExObj.Delete(dp);
_logMessage(this.ToString(), 0, "Conclusa attività pulizia con esito positivo!");
_logMessageDbLog(this.ToString(), 0, "Conclusa attività pulizia con esito positivo!");
//dp = new DynamicParameters();
//dp.Add("@keyid", _log.Keyid, DbType.Guid, ParameterDirection.Input);
//ActionExTask.Delete(dp);
}
catch (Exception ex)
{
result = ex.HResult;
_logMessage(this.ToString(), ex.HResult, ex.Message);
_logMessageDbLog(this.ToString(), ex.HResult, ex.Message);
}
return result;
}
public long Start(string action, string parms)
{
long result = 0;
this.InsertTask("Task Start!");
_logMessage(this.ToString(), 0, string.Format("Start task {0} with path [{0}]", Name, parms));
_logMessageDbLog(this.ToString(), 0, string.Format("Start task {0} with path [{0}]", Name, parms));
switch (action.ToLower())
{
case "start cleaning task":
Cleaning();
break;
case "start explorer task":
cacheListFiles = new EntityList<ExplorerObjects>();
cacheListDirs = new EntityList<ExplorerObjects>();
result = DirectorySearch(parms);
break;
default:
break;
}
Stop(parms);
return result;
}
public void Stop(string parms)
{
_logMessage(this.ToString(), 0, string.Format("Finish task {0} with path [{1}]", Name, parms));
_logMessageDbLog(this.ToString(), 0, string.Format("Finish task {0} with path [{1}]", Name, parms));
this.FinishTask("Task Stop!");
_bStopped = true;
}
private long DirectorySearch(string path)
{
try
{
if (_bStopped)
return NIterations;
if (!Directory.Exists(path))
{
_logMessage(this.ToString(), -1, "That path to directory not exists.");
_logMessageDbLog(this.ToString(), -1, "That path to directory not exists.");
return -1;
}
string[] root = path.Split(':');
DriveInfo dr = new DriveInfo(root[0]);
foreach (string fname in Directory.GetFiles(path))
{
try
{
FileInfo fi = new FileInfo(fname);
cacheListFiles.Add(new ExplorerObjects()
{
Keyid = Guid.NewGuid(),
Fullpath = fi.FullName,
Drive = dr.RootDirectory.Name,
Name = fi.Name,
IsDirectory = 0,
Length = fi.Length,
Attributes = fi.Attributes.ToString(),
DirectoryName = fi.DirectoryName,
Extension = fi.Extension,
FileCreationTime = Program.EnsureSQLSafe(fi.CreationTime),
FileLastAccessTime = Program.EnsureSQLSafe(fi.LastAccessTime),
FileLastWriteTime = Program.EnsureSQLSafe(fi.LastWriteTime)
});
if(cacheListFiles.Count >= Program.MASSIVECOUNT)
{
ActionExObj.MassiveInsert(cacheListFiles);
cacheListFiles = new EntityList<ExplorerObjects>();
}
NIterations += 1;
}
catch (Exception ex)
{
_logMessage(this.ToString(), ex.HResult, ex.Message + ": " + fname);
_logMessageDbLog(this.ToString(), ex.HResult, ex.Message + ": " + fname);
}
}
if (cacheListFiles.Count > 0)
{
ActionExObj.MassiveInsert(cacheListFiles);
cacheListFiles = new EntityList<ExplorerObjects>();
}
foreach (string dname in Directory.GetDirectories(path))
{
try
{
DirectoryInfo di = new DirectoryInfo(dname);
cacheListDirs.Add(new ExplorerObjects()
{
Keyid = Guid.NewGuid(),
Fullpath = di.FullName,
Drive = dr.RootDirectory.Name,
Name = di.Name,
IsDirectory = 1,
Length = 0,
Attributes = di.Attributes.ToString(),
DirectoryName = "",
Extension = di.Extension,
FileCreationTime = Program.EnsureSQLSafe(di.CreationTime),
FileLastAccessTime = Program.EnsureSQLSafe(di.LastAccessTime),
FileLastWriteTime = Program.EnsureSQLSafe(di.LastWriteTime)
});
if (cacheListDirs.Count >= Program.MASSIVECOUNT)
{
ActionExObj.MassiveInsert(cacheListDirs);
cacheListDirs = new EntityList<ExplorerObjects>();
}
NIterations += 1;
}
catch(Exception ex)
{
_logMessage(this.ToString(), ex.HResult, ex.Message + ": " + dname);
_logMessageDbLog(this.ToString(), ex.HResult, ex.Message + ": " + dname);
}
DirectorySearch(dname);
}
if (cacheListDirs.Count > 0)
{
ActionExObj.MassiveInsert(cacheListDirs);
cacheListDirs = new EntityList<ExplorerObjects>();
}
}
catch (Exception ex)
{
_logMessage(this.ToString(), ex.HResult, ex.Message);
_logMessageDbLog(this.ToString(), ex.HResult, ex.Message);
}
return NIterations;
}
public override string ToString()
{
return this.GetType().Name;
}
}
}