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