-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
483 lines (443 loc) · 11.7 KB
/
Program.cs
File metadata and controls
483 lines (443 loc) · 11.7 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Diagnostics;
using System.ComponentModel;
using System.IO;
using System.Text;
using System.Linq;
using System.Threading;
namespace DataTests
{
partial class Program
{
protected static Type m_type = typeof(Program);
protected static List<MethodInfo> m_methods = null;
[STAThread]
static void Main(string[] args)
{
CreateMethodCache();
AddTraceListeners();
Console.Clear();
Console.ForegroundColor = ConsoleColor.White;
Console.BufferHeight = 300;
Console.BufferWidth = 100;
Console.Title = "Test Bench";
if (args.Length > 0)
{
RunCommand(args);
return;
}
bool pContinue = true;
while (pContinue)
{
Console.Write(":>");
string[] Coms = Console.ReadLine().Split(new char[] { ' ' });
if (Coms.Length == 0)
continue;
switch (Coms[0].ToLower())
{
case "exit":
case "quit":
pContinue = false;
break;
default:
RunCommand(Coms);
break;
}
Console.WriteLine("");
}
}
static void RunCommand(string[] args)
{
string CallingMethod = args[0];
object[] param = new object[] { };
if (args.Length > 1)
{
//Remove the First Param which should be the calling Method
param = new object[args.Length - 1];
for (int i = 0; i < param.Length; i++)
{
int j = i;
param[i] = args[++j];
}
}
foreach (MethodInfo mi in m_methods)
{
if (
// Validate the File Name
String.Compare(mi.Name.ToLower(), CallingMethod.ToLower(), true) != 0
// Validate the Parameter Count
|| mi.GetParameters().Length != param.Length
)
{
continue;
}
try
{
object prog = System.Activator.CreateInstance(m_type);
m_type.InvokeMember(mi.Name, BindingFlags.Default | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.InvokeMethod, null, prog, param);
return;
}
catch (Exception e)
{
if (e.InnerException != null)
{
Console.WriteLine();
WriteExceptions(e.InnerException);
}
return;
}
}
// If we fell out of the loop and didn't find a Matching Method, then Call Help.
Console.WriteLine("Unknown Command");
Program.Help();
}
[Description("Shows Help for All Commands")]
static void Help()
{
Console.WriteLine("Valid Commands");
foreach (MethodInfo m in m_methods)
{
DescriptionAttribute[] attribs = (DescriptionAttribute[])m.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attribs != null && attribs.Length > 0)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.Write(m.Name);
ParameterInfo[] parm = m.GetParameters();
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write("(");
for (int i = 0; i < parm.Length; i++)
{
if (i > 0)
Console.Write(", ");
Console.Write("({0}){1}", parm[i].ParameterType.Name, parm[i].Name);
}
Console.Write(")");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("\n\t{0}", attribs[0].Description);
}
}
}
[Description("Clears the Current display Buffer")]
static void Clear()
{
Console.Clear();
}
[Description("Quits out of the application")]
static void Quit()
{
return;
}
[Description("List Local Drives")]
static void LocalDrives()
{
string[] drives = Environment.GetLogicalDrives();
IEnumerable<string> strs = drives.Select(s => s.Replace(":\\", ""));
foreach (String s in strs)
{
System.IO.DriveInfo drvi = new System.IO.DriveInfo(s);
if (drvi.DriveType == DriveType.CDRom)
continue;
Console.WriteLine("{0}:\\", s);
}
}
[Description("List Available Providers")]
static void LocalProviders()
{
var dt = System.Data.Common.DbProviderFactories.GetFactoryClasses();
// Name Description InvariantName
Console.WriteLine("{0} {1} {2}", "", "Name", "InvariantName");
Console.WriteLine("-------------------------------------");
foreach (System.Data.DataRow dr in dt.Rows)
{
Console.WriteLine("{0} {1} {2}", "", dr["Name"], dr["InvariantName"]);
}
}
[Description("Open Application Log Folder")]
static void OpenLogFolder()
{
Process.Start(Path.Combine(GetCurrentPath(), "ApplicationLogs"));
}
[Description("Menu Sample")]
static void SampleMenu()
{
List<MenuChoice> _choices = new List<MenuChoice>();
_choices.Add(new MenuChoice(new System.Action(Program.LocalDrives), "List Local Drives"));
_choices.Add(new MenuChoice(new System.Action(Program.OpenLogFolder), "Opens the Current Log Folder"));
_choices.Add(new MenuChoice(new System.Action(Program.Help), "Help"));
_choices.Add(new MenuChoice(new System.Action(Program.LocalProviders), "List Available Providers"));
Menu m = new Menu(_choices);
m.RunMenu();
if (m.Canceled)
return;
foreach (MenuChoice mc in _choices)
{
if (mc.Selected == false)
continue;
mc.InvokeAction();
}
}
static void ls()
{
var path = @"C:\Windows";
var di = new DirectoryInfo(path);
foreach (var file in di.EnumerateFiles())
{
Console.WriteLine(file.Name);
}
}
static TimeSpan CalculateEta(DateTime startTime, int totalItems, int completeItems)
{
TimeSpan _eta = TimeSpan.MinValue;
// Avoid Divide by Zero Errors
if (completeItems > 0)
{
int _itemduration = (int)DateTime.Now.Subtract(startTime).TotalMilliseconds / completeItems;
_eta = TimeSpan.FromMilliseconds((double)((totalItems - completeItems) * _itemduration));
}
return _eta;
}
static void WriteExceptions(Exception e)
{
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.Red;
Trace.Write("Source:");
Trace.Write(e.Source);
Trace.WriteLine("\nMessage:");
Trace.Write(e.Message);
Trace.WriteLine("\nStack Trace:");
Trace.Write(e.StackTrace);
Trace.WriteLine("\nUser Defined Data:");
foreach (System.Collections.DictionaryEntry de in e.Data)
{
Trace.WriteLine(string.Format("[{0}] :: {1}", de.Key, de.Value));
}
if (e.InnerException != null)
{
WriteExceptions(e.InnerException);
}
Console.ForegroundColor = ConsoleColor.White;
}
static string GetCurrentPath()
{
var asm = Assembly.GetExecutingAssembly();
var fi = new FileInfo(asm.Location);
return fi.DirectoryName;
}
static void HexDump(byte[] bytes)
{
for (int line = 0; line < bytes.Length; line += 16)
{
byte[] lineBytes = bytes.Skip(line).Take(16).ToArray();
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.AppendFormat("{0:x8} ", line);
sb.Append(string.Join(" ", lineBytes.Select(b => b.ToString("x2")).ToArray()).PadRight(16 * 3));
sb.Append(" ");
sb.Append(new string(lineBytes.Select(b => b < 32 ? '.' : (char)b).ToArray()));
Console.WriteLine(sb);
}
}
static void CreateMethodCache()
{
m_methods = m_type.GetMethods(BindingFlags.Static | BindingFlags.NonPublic).ToList();
}
static void AddTraceListeners()
{
var lt = new LogFileTraceListener();
TextWriterTraceListener CWriter = new TextWriterTraceListener(Console.Out);
Trace.Listeners.Add(CWriter);
Trace.Listeners.Add(lt);
}
internal class LogFileTraceListener : TraceListener
{
private readonly static int m_logFileSize = 1048576;
private readonly static int m_logstoKeep = 10;
public LogFileTraceListener()
{
this.Name = "LogFileTracing";
this.LogName = GetApplicationName();
this.LogRootPath = Path.Combine(GetBaseDirectory(), "ApplicationLogs");
this.CheckDirectory();
this.RenameLogFiles();
this.CheckLogFile();
}
public string LogName { get; set; }
public string LogRootPath { get; set; }
private string LogFile
{
get
{
return Path.Combine(this.LogRootPath, GetLogFileName(0));
}
}
public override void Write(string message)
{
this.CheckLogFile();
lock (this)
{
try
{
using (var sw = new StreamWriter(this.LogFile, true))
{
sw.Write(string.Format("{0} :: {1}", DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss"), message.Trim()));
sw.Flush();
}
}
catch (Exception e)
{
Trace.Listeners.Remove(this.Name);
Trace.WriteLine("EXCEPTION IN Write:");
Trace.WriteLine(e.Message);
}
}
}
public override void WriteLine(string message)
{
this.CheckLogFile();
lock (this)
{
try
{
using (var sw = new StreamWriter(this.LogFile, true))
{
sw.WriteLine(string.Format("{0} :: {1}", DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss"), message.Trim()));
sw.Flush();
}
}
catch (Exception e)
{
Trace.Listeners.Remove(this.Name);
Trace.WriteLine("EXCEPTION IN WriteLine:");
Trace.WriteLine(e.Message);
}
}
}
public void CheckLogFile()
{
if (!File.Exists(this.LogFile))
{
CreateLogFile();
}
else
{
FileInfo fi = new FileInfo(this.LogFile);
if (fi.Length > m_logFileSize)
{
RenameLogFiles();
CreateLogFile();
}
}
}
private void CreateLogFile()
{
if (File.Exists(this.LogFile))
{
return;
}
try
{
CheckDirectory();
using (FileStream fs = new FileStream(this.LogFile, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
fs.Flush();
fs.Close();
}
}
catch (Exception e)
{
Trace.Listeners.Remove(this.Name);
Trace.WriteLine("EXCEPTION IN CreateLogFile:");
Trace.WriteLine(e.Message);
}
}
private void RenameLogFiles()
{
// Make room for our files.
string maxFile = Path.Combine(LogRootPath, GetLogFileName(m_logstoKeep));
if (File.Exists(maxFile))
{
File.Delete(maxFile);
}
DirectoryInfo di = new DirectoryInfo(LogRootPath);
var fic = di.GetFiles(LogName + "*.log");
int i = m_logstoKeep;
if (fic.Length > 0)
{
while (i != 0)
{
int j = i + 1;
string v_currfile = Path.Combine(LogRootPath, GetLogFileName(i));
string v_destfile = Path.Combine(LogRootPath, GetLogFileName(j));
if (File.Exists(v_currfile))
{
if (File.Exists(v_destfile))
{
File.Delete(v_destfile);
}
File.Move(v_currfile, v_destfile);
}
i--;
}
string logFile = Path.Combine(LogRootPath, GetLogFileName(0));
if (File.Exists(logFile))
File.Move(logFile, Path.Combine(LogRootPath, GetLogFileName(1)));
}
}
private void CheckDirectory()
{
try
{
if (Directory.Exists(LogRootPath) == false)
{
Directory.CreateDirectory(LogRootPath);
}
}
catch (Exception e)
{
Trace.Listeners.Remove(this.Name);
Trace.WriteLine("EXCEPTION IN CheckDirectory:");
Trace.WriteLine(e.Message);
}
}
private string GetBaseDirectory()
{
string basedir = string.Empty;
try
{
var assm = Assembly.GetEntryAssembly();
var fi = new FileInfo(assm.Location);
basedir = fi.DirectoryName;
}
catch (Exception e)
{
Trace.Listeners.Remove(this.Name);
Trace.WriteLine("EXCEPTION IN GetExecutingDirectory:");
Trace.WriteLine(e.Message);
}
return basedir;
}
private string GetApplicationName()
{
try
{
Assembly asm = Assembly.GetEntryAssembly();
if (asm == null)
return "ApplicationTraceFile";
return Assembly.GetEntryAssembly().GetName().Name;
}
catch (Exception e)
{
Trace.Listeners.Remove(this.Name);
Trace.WriteLine("EXCEPTION IN GetApplicationName:");
Trace.WriteLine(e.Message);
}
return String.Empty;
}
private string GetLogFileName(int n)
{
return String.Format("{0}{1}.log", this.LogName, (n == 0) ? "" : n.ToString());
}
}
}
}