Winify – Blame information for rev 38

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