Winify – Diff between revs 28 and 30
?pathlinks?
Rev 28 | Rev 30 | |||
---|---|---|---|---|
1 | using System; |
1 | using System; |
|
2 | using System.Collections.Generic; |
2 | using System.Collections.Generic; |
|
- | 3 | using System.Diagnostics; |
||
3 | using System.Drawing; |
4 | using System.Drawing; |
|
4 | using System.IO; |
5 | using System.IO; |
|
5 | using System.Reflection; |
6 | using System.Reflection; |
|
6 | using System.Runtime.InteropServices; |
7 | using System.Threading; |
|
7 | using System.Threading.Tasks; |
8 | using System.Threading.Tasks; |
|
8 | using System.Windows.Forms; |
9 | using System.Windows.Forms; |
|
9 | using Microsoft.Win32; |
10 | using Microsoft.Win32; |
|
10 | |
11 | |
|
11 | namespace Winify.Utilities |
12 | namespace Winify.Utilities |
|
12 | { |
13 | { |
|
13 | public static class Miscellaneous |
14 | public static class Miscellaneous |
|
14 | { |
15 | { |
|
15 | #region Public Methods |
16 | #region Public Methods |
|
16 | |
- | ||
17 | public static TimeSpan GetIdleTime() |
- | ||
18 | { |
- | ||
19 | var lastInPut = new Natives.LASTINPUTINFO(); |
- | ||
20 | lastInPut.cbSize = (uint)Marshal.SizeOf(lastInPut); |
- | ||
21 | Natives.GetLastInputInfo(ref lastInPut); |
- | ||
22 | |
- | ||
23 | return TimeSpan.FromMilliseconds((uint)Environment.TickCount - lastInPut.dwTime); |
- | ||
24 | } |
- | ||
25 | |
17 | |
|
26 | public static bool LaunchOnBootSet(bool enable) |
18 | public static bool LaunchOnBootSet(bool enable) |
|
27 | { |
19 | { |
|
28 | using (var key = Registry.CurrentUser.OpenSubKey |
20 | using var key = Registry.CurrentUser.OpenSubKey |
|
29 | ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true)) |
21 | ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); |
|
30 | { |
22 | |
|
31 | if (key == null) return false; |
23 | if (key == null) return false; |
|
32 | |
24 | |
|
33 | switch (enable) |
25 | switch (enable) |
|
34 | { |
26 | { |
|
35 | case true: |
27 | case true: |
|
36 | key.SetValue(Constants.AssemblyName, Assembly.GetEntryAssembly().Location); |
28 | key.SetValue(Constants.AssemblyName, Assembly.GetEntryAssembly().Location); |
|
37 | break; |
29 | break; |
|
38 | default: |
30 | default: |
|
39 | key.DeleteValue(Constants.AssemblyName, false); |
31 | key.DeleteValue(Constants.AssemblyName, false); |
|
40 | break; |
32 | break; |
|
41 | } |
33 | } |
|
42 | } |
- | ||
43 | |
34 | |
|
44 | return true; |
35 | return true; |
|
45 | } |
36 | } |
|
46 | |
37 | |
|
47 | public static bool LaunchOnBootGet() |
38 | public static bool LaunchOnBootGet() |
|
48 | { |
39 | { |
|
49 | using (var key = Registry.CurrentUser.OpenSubKey |
40 | using var key = Registry.CurrentUser.OpenSubKey |
|
50 | ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true)) |
41 | ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); |
|
51 | { |
42 | |
|
52 | return key?.GetValue(Constants.AssemblyName) != null; |
43 | return key?.GetValue(Constants.AssemblyName) != null; |
|
53 | } |
44 | } |
|
54 | } |
- | ||
55 | |
45 | |
|
56 | /// <summary> |
46 | /// <summary> |
|
57 | /// Enable double buffering for an arbitrary control. |
47 | /// Enable double buffering for an arbitrary control. |
|
58 | /// </summary> |
48 | /// </summary> |
|
59 | /// <param name="control">the control to enable double buffering for</param> |
49 | /// <param name="control">the control to enable double buffering for</param> |
|
60 | /// <returns>true on success</returns> |
50 | /// <returns>true on success</returns> |
|
61 | /// <remarks>Do not enable double buffering on RDP: https://devblogs.microsoft.com/oldnewthing/20060103-12/?p=32793</remarks> |
51 | /// <remarks>Do not enable double buffering on RDP: https://devblogs.microsoft.com/oldnewthing/20060103-12/?p=32793</remarks> |
|
62 | public static bool SetDoubleBuffered(this Control control) |
52 | public static void SetDoubleBuffered(this Control control) |
|
- | 53 | { |
||
- | 54 | // Double buffering can make DGV slow in remote desktop |
||
- | 55 | if (!SystemInformation.TerminalServerSession) |
||
63 | { |
56 | { |
|
64 | if (SystemInformation.TerminalServerSession) return false; |
- | ||
65 | |
- | ||
66 | var dgvType = control.GetType(); |
57 | var dgvType = control.GetType(); |
|
67 | var pi = dgvType.GetProperty("DoubleBuffered", |
58 | var pi = dgvType.GetProperty("DoubleBuffered", |
|
68 | BindingFlags.Instance | BindingFlags.NonPublic); |
59 | BindingFlags.Instance | BindingFlags.NonPublic); |
|
69 | if (pi == null) return false; |
- | ||
70 | |
- | ||
71 | pi.SetValue(control, true, null); |
60 | pi.SetValue(control, true, null); |
|
72 | |
- | ||
73 | return true; |
61 | } |
|
74 | } |
62 | } |
|
75 | |
63 | |
|
76 | public static async Task<Icon> CreateIconFromResource(string resource) |
64 | public static async Task<Icon> CreateIconFromResource(string resource) |
|
77 | { |
65 | { |
|
78 | var iconBytes = await LoadResource(resource); |
66 | var iconBytes = await LoadResource(resource); |
|
79 | using (var iconMemoryStream = new MemoryStream(iconBytes)) |
67 | using var iconMemoryStream = new MemoryStream(iconBytes); |
|
80 | { |
- | ||
81 | var bitmap = (Bitmap)Image.FromStream(iconMemoryStream); |
68 | var bitmap = (Bitmap)Image.FromStream(iconMemoryStream); |
|
82 | var bitmapIntPtr = bitmap.GetHicon(); |
69 | var bitmapIntPtr = bitmap.GetHicon(); |
|
83 | var icon = Icon.FromHandle(bitmapIntPtr); |
70 | var icon = Icon.FromHandle(bitmapIntPtr); |
|
84 | return icon; |
71 | return icon; |
|
85 | } |
72 | } |
|
86 | } |
- | ||
87 | |
73 | |
|
88 | public static async Task<byte[]> LoadResource(string resource) |
74 | public static async Task<byte[]> LoadResource(string resource) |
|
89 | { |
75 | { |
|
90 | var assembly = Assembly.GetExecutingAssembly(); |
76 | var assembly = Assembly.GetExecutingAssembly(); |
|
91 | |
77 | |
|
92 | using (var manifestResourceStream = assembly.GetManifestResourceStream(resource)) |
78 | using var manifestResourceStream = assembly.GetManifestResourceStream(resource); |
|
93 | { |
79 | |
|
94 | if (manifestResourceStream == null) return null; |
80 | if (manifestResourceStream == null) return null; |
|
95 | |
81 | |
|
96 | var memoryStream = new MemoryStream(); |
82 | var memoryStream = new MemoryStream(); |
|
97 | |
83 | |
|
98 | await manifestResourceStream.CopyToAsync(memoryStream); |
84 | await manifestResourceStream.CopyToAsync(memoryStream); |
|
99 | |
85 | |
|
100 | memoryStream.Position = 0L; |
86 | memoryStream.Position = 0L; |
|
101 | |
87 | |
|
102 | return memoryStream.ToArray(); |
88 | return memoryStream.ToArray(); |
|
103 | } |
89 | } |
|
104 | } |
- | ||
105 | |
90 | |
|
106 | public static void InvokeIfRequired<T>(this T control, Action<T> action) where T : Control |
91 | public static void InvokeIfRequired<T>(this T control, Action<T> action) where T : Control |
|
107 | { |
92 | { |
|
108 | if (control.InvokeRequired) |
93 | if (control.InvokeRequired) |
|
109 | { |
94 | { |
|
110 | control.BeginInvoke((MethodInvoker)delegate { action(control); }); |
95 | control.BeginInvoke((MethodInvoker)delegate { action(control); }); |
|
111 | return; |
96 | return; |
|
112 | } |
97 | } |
|
113 | |
98 | |
|
114 | action(control); |
99 | action(control); |
|
115 | } |
100 | } |
|
- | 101 | |
||
116 | |
102 | /// <summary> |
|
117 | public static T MapValueToRange<T>(this T value, T xMin, T xMax, T yMin, T yMax) |
103 | /// Attempts to access a file within <see cref="timeout" /> milliseconds by retrying to access the file every |
|
118 | where T : struct, IComparable<T>, IConvertible |
104 | /// <see cref="retry" /> milliseconds. |
|
119 | { |
105 | /// </summary> |
|
120 | return (dynamic)yMin + |
106 | /// <param name="path">the path to the file</param> |
|
121 | ((dynamic)yMax - (dynamic)yMin) * ((dynamic)value - (dynamic)xMin) / |
107 | /// <param name="mode">the file mode used to access the file</param> |
|
122 | ((dynamic)xMax - (dynamic)xMin); |
108 | /// <param name="access">the file access to use</param> |
|
123 | } |
- | ||
- | 109 | /// <param name="share">the file share to use</param> |
||
- | 110 | /// <param name="cancellationToken">a cancellation token</param> |
||
- | 111 | /// <param name="retry">the amount of milliseconds to retry between accesses to the file</param> |
||
- | 112 | /// <param name="timeout">the amount of time in milliseconds to attempt and access the file</param> |
||
124 | |
113 | /// <returns>a file stream if the file could be accessed within the allotted time</returns> |
|
125 | public static IEnumerable<TU> SequenceSubtract<TU, TV>(this IEnumerable<TU> a, |
114 | public static async Task<FileStream> GetFileStream(string path, FileMode mode, FileAccess access, |
|
126 | IEnumerable<TV> b, |
115 | FileShare share, CancellationToken cancellationToken, |
|
127 | Func<TU, TV, bool> cmp) |
116 | int retry = 1000, int timeout = 60000) |
|
128 | { |
117 | { |
|
129 | var eb = new List<TV>(b); |
118 | var time = Stopwatch.StartNew(); |
|
130 | |
119 | |
|
131 | using (var ea = a.GetEnumerator()) |
120 | while (time.ElapsedMilliseconds < timeout && !cancellationToken.IsCancellationRequested) |
|
132 | { |
121 | { |
|
133 | while (ea.MoveNext()) |
- | ||
134 | { |
- | ||
135 | if (ea.Current == null) continue; |
- | ||
136 | |
- | ||
137 | foreach (var ib in eb) |
122 | try |
|
138 | { |
- | ||
139 | if (cmp.Invoke(ea.Current, ib)) continue; |
- | ||
140 | |
- | ||
141 | yield return ea.Current; |
- | ||
142 | |
123 | { |
|
- | 124 | return new FileStream(path, mode, access, share); |
||
- | 125 | } |
||
- | 126 | catch (IOException e) |
||
- | 127 | { |
||
143 | break; |
128 | // access error |
|
- | 129 | if (e.HResult != -2147024864) throw; |
||
- | 130 | } |
||
144 | } |
131 | |
|
- | 132 | await Task.Delay(retry, cancellationToken); |
||
- | 133 | } |
||
145 | } |
134 | |
|
146 | } |
135 | throw new TimeoutException($"Failed to get a access to {path} within {timeout}ms."); |
|
147 | } |
136 | } |
|
148 | |
137 | |
|
149 | /// <summary> |
138 | /// <summary> |
|
150 | /// Returns the machine GUID. |
139 | /// Returns the machine GUID. |
|
151 | /// </summary> |
140 | /// </summary> |
|
152 | /// <returns>the GUID of the machine</returns> |
141 | /// <returns>the GUID of the machine</returns> |
|
153 | /// <remarks>https://stackoverflow.com/questions/2333149/how-to-fast-get-hardware-id-in-c</remarks> |
142 | /// <remarks>https://stackoverflow.com/questions/2333149/how-to-fast-get-hardware-id-in-c</remarks> |
|
154 | public static string GetMachineGuid() |
143 | public static string GetMachineGuid() |
|
155 | { |
144 | { |
|
156 | var location = @"SOFTWARE\Microsoft\Cryptography"; |
145 | var location = @"SOFTWARE\Microsoft\Cryptography"; |
|
157 | var name = "MachineGuid"; |
146 | var name = "MachineGuid"; |
|
158 | |
147 | |
|
159 | using (var localMachineX64View = |
148 | using (var localMachineX64View = |
|
160 | RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)) |
149 | RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)) |
|
161 | { |
150 | { |
|
162 | using (var rk = localMachineX64View.OpenSubKey(location)) |
151 | using (var rk = localMachineX64View.OpenSubKey(location)) |
|
163 | { |
152 | { |
|
164 | if (rk == null) |
153 | if (rk == null) |
|
165 | throw new KeyNotFoundException( |
154 | throw new KeyNotFoundException( |
|
166 | string.Format("Key Not Found: {0}", location)); |
155 | string.Format("Key Not Found: {0}", location)); |
|
167 | |
156 | |
|
168 | var machineGuid = rk.GetValue(name); |
157 | var machineGuid = rk.GetValue(name); |
|
169 | if (machineGuid == null) |
158 | if (machineGuid == null) |
|
170 | throw new IndexOutOfRangeException( |
159 | throw new IndexOutOfRangeException( |
|
171 | string.Format("Index Not Found: {0}", name)); |
160 | string.Format("Index Not Found: {0}", name)); |
|
172 | |
161 | |
|
173 | return machineGuid.ToString(); |
162 | return machineGuid.ToString(); |
|
174 | } |
163 | } |
|
175 | } |
164 | } |
|
176 | } |
165 | } |
|
177 | |
166 | |
|
178 | #endregion |
167 | #endregion |
|
179 | } |
168 | } |
|
180 | } |
169 | } |
|
181 | |
170 | |
|
182 |
|
171 |
|
|
183 | |
172 | |
|
184 | |
173 | |
|
185 | |
174 | |