Winify – Diff between revs 38 and 61

Subversion Repositories:
Rev:
Only display areas with differencesIgnore whitespace
Rev 38 Rev 61
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 { -  
23 if (key == null) return false; 22 if (key == null) return false;
24   -  
25 switch (enable) -  
26 { -  
27 case true: -  
28 key.SetValue(Constants.AssemblyName, Assembly.GetEntryAssembly().Location); -  
29 break; -  
30 default: -  
31 key.DeleteValue(Constants.AssemblyName, false); -  
32 break; -  
33 } -  
-   23  
-   24 switch (enable)
34   25 {
-   26 case true:
-   27 key.SetValue(Constants.AssemblyName, Assembly.GetEntryAssembly().Location);
-   28 break;
-   29 default:
-   30 key.DeleteValue(Constants.AssemblyName, false);
35 return true; 31 break;
-   32 }
-   33  
36 } 34 return true;
37 } 35 }
38   36  
39 public static bool LaunchOnBootGet() 37 public static bool LaunchOnBootGet()
40 { 38 {
41 using (var key = Registry.CurrentUser.OpenSubKey 39 using var key = Registry.CurrentUser.OpenSubKey
42 ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true)) 40 ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
43 { -  
44 return key?.GetValue(Constants.AssemblyName) != null; 41 return key?.GetValue(Constants.AssemblyName) != null;
45 } -  
46 } 42 }
47   43  
48 /// <summary> 44 /// <summary>
49 /// Enable double buffering for an arbitrary control. 45 /// Enable double buffering for an arbitrary control.
50 /// </summary> 46 /// </summary>
51 /// <param name="control">the control to enable double buffering for</param> 47 /// <param name="control">the control to enable double buffering for</param>
52 /// <returns>true on success</returns> 48 /// <returns>true on success</returns>
53 /// <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>
54 public static void SetDoubleBuffered(this Control control) 50 public static void SetDoubleBuffered(this Control control)
55 { 51 {
56 // Double buffering can make DGV slow in remote desktop 52 // Double buffering can make DGV slow in remote desktop
57 if (!SystemInformation.TerminalServerSession) 53 if (!SystemInformation.TerminalServerSession)
58 { 54 {
59 var dgvType = control.GetType(); 55 var dgvType = control.GetType();
60 var pi = dgvType.GetProperty("DoubleBuffered", 56 var pi = dgvType.GetProperty("DoubleBuffered",
61 BindingFlags.Instance | BindingFlags.NonPublic); 57 BindingFlags.Instance | BindingFlags.NonPublic);
62 pi.SetValue(control, true, null); 58 pi.SetValue(control, true, null);
63 } 59 }
64 } 60 }
65   61  
66 public static async Task<Icon> CreateIconFromResource(string resource) 62 public static async Task<Icon> CreateIconFromResource(string resource)
67 { 63 {
68 var iconBytes = await LoadResource(resource); 64 var iconBytes = await LoadResource(resource);
69 using (var iconMemoryStream = new MemoryStream(iconBytes)) 65 using var iconMemoryStream = new MemoryStream(iconBytes);
70 { -  
71 var bitmap = (Bitmap)Image.FromStream(iconMemoryStream); 66 var bitmap = (Bitmap)Image.FromStream(iconMemoryStream);
72 var bitmapIntPtr = bitmap.GetHicon(); 67 var bitmapIntPtr = bitmap.GetHicon();
73 var icon = Icon.FromHandle(bitmapIntPtr); 68 var icon = Icon.FromHandle(bitmapIntPtr);
74 return icon; 69 return icon;
75 } -  
76 } 70 }
77   71  
78 public static async Task<byte[]> LoadResource(string resource) 72 public static async Task<byte[]> LoadResource(string resource)
79 { 73 {
80 var assembly = Assembly.GetExecutingAssembly(); 74 var assembly = Assembly.GetExecutingAssembly();
81   75  
82 using (var manifestResourceStream = assembly.GetManifestResourceStream(resource)) -  
83 { 76 using var manifestResourceStream = assembly.GetManifestResourceStream(resource);
84 if (manifestResourceStream == null) return null; 77 if (manifestResourceStream == null) return null;
85   78  
86 var memoryStream = new MemoryStream(); 79 var memoryStream = new MemoryStream();
87   80  
88 await manifestResourceStream.CopyToAsync(memoryStream); -  
89   81 await manifestResourceStream.CopyToAsync(memoryStream);
90 memoryStream.Position = 0L; 82  
91   83 memoryStream.Position = 0L;
92 return memoryStream.ToArray(); 84  
93 } 85 return memoryStream.ToArray();
94 } 86 }
95   87  
96 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
97 { 89 {
98 if (control.InvokeRequired) 90 if (control.InvokeRequired)
99 { 91 {
100 control.BeginInvoke((MethodInvoker)delegate { action(control); }); 92 control.BeginInvoke((MethodInvoker)delegate { action(control); });
101 return; 93 return;
102 } 94 }
103   95  
104 action(control); 96 action(control);
105 } 97 }
106   98  
107 /// <summary> 99 /// <summary>
108 /// 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
109 /// <see cref="retry" /> milliseconds. 101 /// <see cref="retry" /> milliseconds.
110 /// </summary> 102 /// </summary>
111 /// <param name="path">the path to the file</param> 103 /// <param name="path">the path to the file</param>
112 /// <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>
113 /// <param name="access">the file access to use</param> 105 /// <param name="access">the file access to use</param>
114 /// <param name="share">the file share to use</param> 106 /// <param name="share">the file share to use</param>
115 /// <param name="cancellationToken">a cancellation token</param> 107 /// <param name="cancellationToken">a cancellation token</param>
116 /// <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>
117 /// <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>
118 /// <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>
119 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,
120 FileShare share, CancellationToken cancellationToken, 112 FileShare share, CancellationToken cancellationToken,
121 int retry = 1000, int timeout = 60000) 113 int retry = 1000, int timeout = 60000)
122 { 114 {
123 var time = Stopwatch.StartNew(); 115 var time = Stopwatch.StartNew();
124   116  
125 while (time.ElapsedMilliseconds < timeout && !cancellationToken.IsCancellationRequested) 117 while (time.ElapsedMilliseconds < timeout && !cancellationToken.IsCancellationRequested)
126 { 118 {
127 try 119 try
128 { 120 {
129 return new FileStream(path, mode, access, share); 121 return new FileStream(path, mode, access, share);
130 } 122 }
131 catch (IOException e) 123 catch (IOException e)
132 { 124 {
133 // access error 125 // access error
134 if (e.HResult != -2147024864) throw; 126 if (e.HResult != -2147024864) throw;
135 } 127 }
136   128  
137 await Task.Delay(retry, cancellationToken); 129 await Task.Delay(retry, cancellationToken);
138 } 130 }
139   131  
140 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.");
141 } 133 }
142   134  
143 /// <summary> 135 /// <summary>
144 /// Returns the machine GUID. 136 /// Returns the machine GUID.
145 /// </summary> 137 /// </summary>
146 /// <returns>the GUID of the machine</returns> 138 /// <returns>the GUID of the machine</returns>
147 /// <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>
148 public static string GetMachineGuid() 140 public static string GetMachineGuid()
149 { 141 {
150 var location = @"SOFTWARE\Microsoft\Cryptography"; 142 var location = @"SOFTWARE\Microsoft\Cryptography";
151 var name = "MachineGuid"; 143 var name = "MachineGuid";
152   144  
153 using (var localMachineX64View = 145 using var localMachineX64View =
154 RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)) -  
155 { 146 RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
156 using (var rk = localMachineX64View.OpenSubKey(location)) -  
157 { 147 using var rk = localMachineX64View.OpenSubKey(location);
158 if (rk == null) 148 if (rk == null)
159 throw new KeyNotFoundException( 149 throw new KeyNotFoundException(
160 string.Format("Key Not Found: {0}", location)); 150 string.Format("Key Not Found: {0}", location));
161   151  
162 var machineGuid = rk.GetValue(name); 152 var machineGuid = rk.GetValue(name);
163 if (machineGuid == null) 153 if (machineGuid == null)
164 throw new IndexOutOfRangeException( 154 throw new IndexOutOfRangeException(
165 string.Format("Index Not Found: {0}", name)); 155 string.Format("Index Not Found: {0}", name));
166   -  
167 return machineGuid.ToString(); -  
168 } 156  
169 } 157 return machineGuid.ToString();
170 } 158 }
171   159  
172 #endregion 160 #endregion
173 } 161 }
174 } 162 }
175   163  
176
Generated by GNU Enscript 1.6.5.90.
164
Generated by GNU Enscript 1.6.5.90.
177   165  
178   166  
179   167