HamBook – Blame information for rev 13
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | using System; |
13 | office | 2 | using System.ComponentModel; |
1 | office | 3 | using System.Diagnostics; |
4 | using System.Drawing; |
||
5 | using System.IO; |
||
13 | office | 6 | using System.Linq; |
1 | office | 7 | using System.Reflection; |
8 | using System.Threading; |
||
9 | using System.Threading.Tasks; |
||
10 | using System.Windows.Forms; |
||
11 | using Microsoft.Win32; |
||
12 | |||
13 | namespace HamBook.Utilities |
||
14 | { |
||
15 | public static class Miscellaneous |
||
16 | { |
||
17 | #region Public Methods |
||
18 | |||
19 | public static bool LaunchOnBootSet(bool enable) |
||
20 | { |
||
21 | using (var key = Registry.CurrentUser.OpenSubKey |
||
22 | ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true)) |
||
23 | { |
||
24 | if (key == null) |
||
25 | { |
||
26 | return false; |
||
27 | } |
||
28 | |||
29 | switch (enable) |
||
30 | { |
||
31 | case true: |
||
32 | key.SetValue(Constants.AssemblyName, Assembly.GetEntryAssembly().Location); |
||
33 | break; |
||
34 | default: |
||
35 | key.DeleteValue(Constants.AssemblyName, false); |
||
36 | break; |
||
37 | } |
||
38 | |||
39 | return true; |
||
40 | } |
||
41 | } |
||
42 | |||
43 | public static bool LaunchOnBootGet() |
||
44 | { |
||
45 | using (var key = Registry.CurrentUser.OpenSubKey |
||
46 | ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true)) |
||
47 | { |
||
48 | return key?.GetValue(Constants.AssemblyName) != null; |
||
49 | } |
||
50 | } |
||
51 | |||
52 | /// <summary> |
||
53 | /// Enable double buffering for an arbitrary control. |
||
54 | /// </summary> |
||
55 | /// <param name="control">the control to enable double buffering for</param> |
||
56 | /// <returns>true on success</returns> |
||
57 | /// <remarks>Do not enable double buffering on RDP: https://devblogs.microsoft.com/oldnewthing/20060103-12/?p=32793</remarks> |
||
58 | public static void SetDoubleBuffered(this Control control) |
||
59 | { |
||
60 | // Double buffering can make DGV slow in remote desktop |
||
61 | if (!SystemInformation.TerminalServerSession) |
||
62 | { |
||
63 | var dgvType = control.GetType(); |
||
64 | var pi = dgvType.GetProperty("DoubleBuffered", |
||
65 | BindingFlags.Instance | BindingFlags.NonPublic); |
||
66 | pi.SetValue(control, true, null); |
||
67 | } |
||
68 | } |
||
69 | |||
70 | public static async Task<Icon> CreateIconFromResource(string resource) |
||
71 | { |
||
72 | var iconBytes = await LoadResource(resource); |
||
73 | using (var iconMemoryStream = new MemoryStream(iconBytes)) |
||
74 | { |
||
75 | var bitmap = (Bitmap)Image.FromStream(iconMemoryStream); |
||
76 | var bitmapIntPtr = bitmap.GetHicon(); |
||
77 | var icon = Icon.FromHandle(bitmapIntPtr); |
||
78 | return icon; |
||
79 | } |
||
80 | } |
||
81 | |||
82 | public static async Task<byte[]> LoadResource(string resource) |
||
83 | { |
||
84 | var assembly = Assembly.GetExecutingAssembly(); |
||
85 | |||
86 | using (var manifestResourceStream = assembly.GetManifestResourceStream(resource)) |
||
87 | { |
||
88 | if (manifestResourceStream == null) |
||
89 | { |
||
90 | return null; |
||
91 | } |
||
92 | |||
93 | var memoryStream = new MemoryStream(); |
||
94 | |||
95 | await manifestResourceStream.CopyToAsync(memoryStream); |
||
96 | |||
97 | memoryStream.Position = 0L; |
||
98 | |||
99 | return memoryStream.ToArray(); |
||
100 | } |
||
101 | } |
||
102 | |||
103 | public static void InvokeIfRequired<T>(this T control, Action<T> action) where T : Control |
||
104 | { |
||
105 | if (control.InvokeRequired) |
||
106 | { |
||
107 | control.BeginInvoke((MethodInvoker)delegate { action(control); }); |
||
108 | return; |
||
109 | } |
||
110 | |||
111 | action(control); |
||
112 | } |
||
113 | |||
114 | /// <summary> |
||
115 | /// Attempts to access a file within <see cref="timeout" /> milliseconds by retrying to access the file every |
||
116 | /// <see cref="retry" /> milliseconds. |
||
117 | /// </summary> |
||
118 | /// <param name="path">the path to the file</param> |
||
119 | /// <param name="mode">the file mode used to access the file</param> |
||
120 | /// <param name="access">the file access to use</param> |
||
121 | /// <param name="share">the file share to use</param> |
||
122 | /// <param name="cancellationToken">a cancellation token</param> |
||
123 | /// <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> |
||
125 | /// <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, |
||
127 | FileShare share, CancellationToken cancellationToken, |
||
128 | int retry = 1000, int timeout = 60000) |
||
129 | { |
||
130 | var time = Stopwatch.StartNew(); |
||
131 | |||
132 | while (time.ElapsedMilliseconds < timeout && !cancellationToken.IsCancellationRequested) |
||
133 | { |
||
134 | try |
||
135 | { |
||
136 | return new FileStream(path, mode, access, share); |
||
137 | } |
||
138 | catch (IOException e) |
||
139 | { |
||
140 | // access error |
||
141 | if (e.HResult != -2147024864) |
||
142 | { |
||
143 | throw; |
||
144 | } |
||
145 | } |
||
146 | |||
147 | await Task.Delay(retry, cancellationToken); |
||
148 | } |
||
149 | |||
150 | throw new TimeoutException($"Failed to get a access to {path} within {timeout}ms."); |
||
151 | } |
||
152 | |||
13 | office | 153 | /////////////////////////////////////////////////////////////////////////// |
154 | // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // |
||
155 | /////////////////////////////////////////////////////////////////////////// |
||
156 | /// <summary> |
||
157 | /// Get the description from an enumeration value. |
||
158 | /// </summary> |
||
159 | /// <param name="value">an enumeration value</param> |
||
160 | /// <returns>the description or the empty string</returns> |
||
161 | public static string GetDescriptionFromEnumValue(Enum value) |
||
162 | { |
||
163 | var attribute = value.GetType() |
||
164 | .GetRuntimeField(value.ToString()) |
||
165 | .GetCustomAttributes(typeof(DescriptionAttribute), false) |
||
166 | .SingleOrDefault() as DescriptionAttribute; |
||
167 | |||
168 | return attribute?.Description; |
||
169 | } |
||
170 | |||
171 | /////////////////////////////////////////////////////////////////////////// |
||
172 | // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // |
||
173 | /////////////////////////////////////////////////////////////////////////// |
||
174 | /// <summary> |
||
175 | /// Get enumeration value from its name attribute. |
||
176 | /// </summary> |
||
177 | /// <typeparam name="T">the enumeration type</typeparam> |
||
178 | /// <param name="description">the description of a member</param> |
||
179 | /// <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> |
||
181 | public static T GetEnumValueFromDescription<T>(string description, |
||
182 | StringComparison comparison = StringComparison.OrdinalIgnoreCase) |
||
183 | { |
||
184 | var field = typeof(T).GetRuntimeFields() |
||
185 | .SelectMany(f => f.GetCustomAttributes( |
||
186 | typeof(DescriptionAttribute), |
||
187 | false), |
||
188 | ( |
||
189 | f, |
||
190 | a) => new { Field = f, Att = a }) |
||
191 | .SingleOrDefault(a => string.Equals(((DescriptionAttribute)a.Att) |
||
192 | .Description, |
||
193 | description, |
||
194 | comparison)); |
||
195 | |||
196 | return (T)field?.Field.GetValue(Activator.CreateInstance<T>()); |
||
197 | } |
||
198 | |||
1 | office | 199 | #endregion |
200 | } |
||
201 | } |