Winify – Blame information for rev 61

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