Repository navigation
Expand file tree
/
Copy pathUtil.cs
More file actions
130 lines (120 loc) · 4.18 KB
/
Copy pathUtil.cs
File metadata and controls
130 lines (120 loc) · 4.18 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Diagnostics;
using System.Reflection;
using System.Threading.Tasks;
namespace AppWrapper
{
public static class Util
{
/// <summary>
/// Returns string from front of "name - nbr"
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public static string GetStringFromFrontOfText(string text)
{
var ptr = text.IndexOf(" -");
return ptr < 0 ? string.Empty : text.Substring(0, ptr);
}
/// <summary>
/// Return (int)nbr from front of "nbr - string"
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public static int GetNbrFromFrontOfText(string text)
{
var ptr = text.IndexOf(" -");
return int.Parse(text.Substring(0, ptr));
}
public static string GetStringFromEndOfText(string text)
{
var ptr = text.IndexOf("- ") + 2;
return text.Substring(ptr);
}
/// <summary>
/// Delimeter can only be one character, usually one space
/// </summary>
/// <param name="text"></param>
/// <param name="delimiter"></param>
/// <returns></returns>
public static string GetStringFromEndOfText(string text, string delimiter)
{
var ptr = text.IndexOf(delimiter) + 1;
return text.Substring(ptr);
}
/// <summary>
/// Delimeter can only be one character, usually one space
/// </summary>
public static string GetStringFromStatOfText(string text, string delimiter)
{
return text.Substring(0, text.IndexOf(delimiter));
}
const string eol = "\r\n";
public static void LogError(Exception ex, bool showMsgBox, string moduleAndMethod)
{
try
{
string msg = $"Error Message from {moduleAndMethod}: " +
ex.Message + eol +
"Error Type: " + ex.GetType().ToString() + eol +
"Error Details: " + eol + ex.ToString();
if (showMsgBox)
MessageBox.Show(msg, "Program Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
try
{
StackTrace st = new StackTrace(true);
msg += "Stack Trace: " + eol + st.ToString();
}
catch (Exception)
{
}
WriteErrorLog(new StringBuilder(msg));
}
catch (Exception)
{
}
}
public static void LogError(string err, bool showMsgBox, string moduleAndMethod)
{
string msg = $"Error Message from {moduleAndMethod}: " + err + eol;
if (showMsgBox)
MessageBox.Show(msg, "Program Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
WriteErrorLog(new StringBuilder(msg));
}
private static Tuple<string, string> SplitModuleFromMethod(string moduleAndMethod)
{
string[] s = moduleAndMethod.Split('.');
return Tuple.Create(s[0], s.Length > 1 ? s[1] : string.Empty);
}
private static void WriteErrorLog(StringBuilder sb)
{
string fn = GetErrorFilename();
if (fn == null) return;
File.WriteAllText(fn, sb.ToString());
}
private static string GetErrorFilename()
{
try
{
string AppPath = Path.Combine(GetExePath(), "ErrLogs");
var di = new DirectoryInfo(AppPath);
if (!di.Exists)
di.Create();
return AppPath + @"\ErrLog_" + DateTime.Now.ToString("MMddyyyyHHmmss") + ".txt";
}
catch (Exception)
{
return null;
}
}
private static string GetExePath()
{
return Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
}
}
}