Winify – Diff between revs 61 and 67

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