HamBook – Diff between revs 13 and 54
?pathlinks?
Rev 13 | Rev 54 | |||
---|---|---|---|---|
Line 8... | Line 8... | |||
8 | using System.Threading; |
8 | using System.Threading; |
|
9 | using System.Threading.Tasks; |
9 | using System.Threading.Tasks; |
|
10 | using System.Windows.Forms; |
10 | using System.Windows.Forms; |
|
11 | using Microsoft.Win32; |
11 | using Microsoft.Win32; |
|
Line 12... | Line 12... | |||
12 | |
12 | |
|
13 | namespace HamBook.Utilities |
13 | namespace HamBook.Utilities |
|
14 | { |
14 | { |
|
15 | public static class Miscellaneous |
15 | public static class Miscellaneous |
|
16 | { |
16 | { |
|
Line 17... | Line 17... | |||
17 | #region Public Methods |
17 | #region Public Methods |
|
18 | |
18 | |
|
19 | public static bool LaunchOnBootSet(bool enable) |
19 | public static bool LaunchOnBootSet(bool enable) |
|
20 | { |
20 | { |
|
21 | using (var key = Registry.CurrentUser.OpenSubKey |
21 | using (var key = Registry.CurrentUser.OpenSubKey |
|
22 | ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true)) |
- | ||
23 | { |
- | ||
24 | if (key == null) |
22 | ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true)) |
|
25 | { |
- | ||
Line 26... | Line 23... | |||
26 | return false; |
23 | { |
|
27 | } |
24 | if (key == null) return false; |
|
28 | |
25 | |
|
29 | switch (enable) |
26 | switch (enable) |
|
Line 83... | Line 80... | |||
83 | { |
80 | { |
|
84 | var assembly = Assembly.GetExecutingAssembly(); |
81 | var assembly = Assembly.GetExecutingAssembly(); |
|
Line 85... | Line 82... | |||
85 | |
82 | |
|
86 | using (var manifestResourceStream = assembly.GetManifestResourceStream(resource)) |
83 | using (var manifestResourceStream = assembly.GetManifestResourceStream(resource)) |
|
87 | { |
84 | { |
|
88 | if (manifestResourceStream == null) |
- | ||
89 | { |
- | ||
90 | return null; |
- | ||
Line 91... | Line 85... | |||
91 | } |
85 | if (manifestResourceStream == null) return null; |
|
Line 92... | Line 86... | |||
92 | |
86 | |
|
Line 122... | Line 116... | |||
122 | /// <param name="cancellationToken">a cancellation token</param> |
116 | /// <param name="cancellationToken">a cancellation token</param> |
|
123 | /// <param name="retry">the amount of milliseconds to retry between accesses to the file</param> |
117 | /// <param name="retry">the amount of milliseconds to retry between accesses to the file</param> |
|
124 | /// <param name="timeout">the amount of time in milliseconds to attempt and access the file</param> |
118 | /// <param name="timeout">the amount of time in milliseconds to attempt and access the file</param> |
|
125 | /// <returns>a file stream if the file could be accessed within the allotted time</returns> |
119 | /// <returns>a file stream if the file could be accessed within the allotted time</returns> |
|
126 | public static async Task<FileStream> GetFileStream(string path, FileMode mode, FileAccess access, |
120 | public static async Task<FileStream> GetFileStream(string path, FileMode mode, FileAccess access, |
|
127 | FileShare share, CancellationToken cancellationToken, |
121 | FileShare share, CancellationToken cancellationToken, |
|
128 | int retry = 1000, int timeout = 60000) |
122 | int retry = 1000, int timeout = 60000) |
|
129 | { |
123 | { |
|
130 | var time = Stopwatch.StartNew(); |
124 | var time = Stopwatch.StartNew(); |
|
Line 131... | Line 125... | |||
131 | |
125 | |
|
132 | while (time.ElapsedMilliseconds < timeout && !cancellationToken.IsCancellationRequested) |
126 | while (time.ElapsedMilliseconds < timeout && !cancellationToken.IsCancellationRequested) |
|
Line 136... | Line 130... | |||
136 | return new FileStream(path, mode, access, share); |
130 | return new FileStream(path, mode, access, share); |
|
137 | } |
131 | } |
|
138 | catch (IOException e) |
132 | catch (IOException e) |
|
139 | { |
133 | { |
|
140 | // access error |
134 | // access error |
|
141 | if (e.HResult != -2147024864) |
135 | if (e.HResult != -2147024864) throw; |
|
142 | { |
- | ||
143 | throw; |
- | ||
144 | } |
- | ||
145 | } |
136 | } |
|
Line 146... | Line 137... | |||
146 | |
137 | |
|
147 | await Task.Delay(retry, cancellationToken); |
138 | await Task.Delay(retry, cancellationToken); |
|
Line 159... | Line 150... | |||
159 | /// <param name="value">an enumeration value</param> |
150 | /// <param name="value">an enumeration value</param> |
|
160 | /// <returns>the description or the empty string</returns> |
151 | /// <returns>the description or the empty string</returns> |
|
161 | public static string GetDescriptionFromEnumValue(Enum value) |
152 | public static string GetDescriptionFromEnumValue(Enum value) |
|
162 | { |
153 | { |
|
163 | var attribute = value.GetType() |
154 | var attribute = value.GetType() |
|
164 | .GetRuntimeField(value.ToString()) |
155 | .GetRuntimeField(value.ToString()) |
|
165 | .GetCustomAttributes(typeof(DescriptionAttribute), false) |
156 | .GetCustomAttributes(typeof(DescriptionAttribute), false) |
|
166 | .SingleOrDefault() as DescriptionAttribute; |
157 | .SingleOrDefault() as DescriptionAttribute; |
|
Line 167... | Line 158... | |||
167 | |
158 | |
|
168 | return attribute?.Description; |
159 | return attribute?.Description; |
|
Line 169... | Line 160... | |||
169 | } |
160 | } |
|
Line 177... | Line 168... | |||
177 | /// <typeparam name="T">the enumeration type</typeparam> |
168 | /// <typeparam name="T">the enumeration type</typeparam> |
|
178 | /// <param name="description">the description of a member</param> |
169 | /// <param name="description">the description of a member</param> |
|
179 | /// <param name="comparison">the string comparison to use</param> |
170 | /// <param name="comparison">the string comparison to use</param> |
|
180 | /// <returns>the value or the default of T if case no name attribute found</returns> |
171 | /// <returns>the value or the default of T if case no name attribute found</returns> |
|
181 | public static T GetEnumValueFromDescription<T>(string description, |
172 | public static T GetEnumValueFromDescription<T>(string description, |
|
182 | StringComparison comparison = StringComparison.OrdinalIgnoreCase) |
173 | StringComparison comparison = StringComparison.OrdinalIgnoreCase) |
|
183 | { |
174 | { |
|
184 | var field = typeof(T).GetRuntimeFields() |
175 | var field = typeof(T).GetRuntimeFields() |
|
185 | .SelectMany(f => f.GetCustomAttributes( |
176 | .SelectMany(f => f.GetCustomAttributes( |
|
186 | typeof(DescriptionAttribute), |
177 | typeof(DescriptionAttribute), |
|
187 | false), |
178 | false), |
|
188 | ( |
179 | ( |
|
189 | f, |
180 | f, |
|
190 | a) => new { Field = f, Att = a }) |
181 | a) => new { Field = f, Att = a }) |
|
191 | .SingleOrDefault(a => string.Equals(((DescriptionAttribute)a.Att) |
182 | .SingleOrDefault(a => string.Equals(((DescriptionAttribute)a.Att) |
|
192 | .Description, |
183 | .Description, |
|
193 | description, |
184 | description, |
|
194 | comparison)); |
185 | comparison)); |
|
Line 195... | Line 186... | |||
195 | |
186 | |
|
196 | return (T)field?.Field.GetValue(Activator.CreateInstance<T>()); |
187 | return (T)field?.Field.GetValue(Activator.CreateInstance<T>()); |
|
Line 197... | Line 188... | |||
197 | } |
188 | } |