Winify

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ HEAD  →  ?path2? @ 1
/trunk/Winify/Utilities/Miscellaneous.cs
@@ -1,10 +1,9 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;
@@ -15,20 +14,34 @@
{
#region Public Methods
 
public static TimeSpan GetIdleTime()
{
var lastInPut = new Natives.LASTINPUTINFO();
lastInPut.cbSize = (uint) Marshal.SizeOf(lastInPut);
Natives.GetLastInputInfo(ref lastInPut);
 
return TimeSpan.FromMilliseconds((uint) Environment.TickCount - lastInPut.dwTime);
}
 
public static bool LaunchOnBootSet(bool enable)
{
using var key = Registry.CurrentUser.OpenSubKey
("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
if (key == null) return false;
using (var key = Registry.CurrentUser.OpenSubKey
("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
{
if (key == null)
{
return false;
}
 
switch (enable)
{
case true:
key.SetValue(Constants.AssemblyName, Assembly.GetEntryAssembly().Location);
break;
default:
key.DeleteValue(Constants.AssemblyName, false);
break;
switch (enable)
{
case true:
key.SetValue(Constants.AssemblyName, Assembly.GetEntryAssembly().Location);
break;
default:
key.DeleteValue(Constants.AssemblyName, false);
break;
}
}
 
return true;
@@ -36,9 +49,11 @@
 
public static bool LaunchOnBootGet()
{
using var key = Registry.CurrentUser.OpenSubKey
("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
return key?.GetValue(Constants.AssemblyName) != null;
using (var key = Registry.CurrentUser.OpenSubKey
("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
{
return key?.GetValue(Constants.AssemblyName) != null;
}
}
 
/// <summary>
@@ -47,26 +62,36 @@
/// <param name="control">the control to enable double buffering for</param>
/// <returns>true on success</returns>
/// <remarks>Do not enable double buffering on RDP: https://devblogs.microsoft.com/oldnewthing/20060103-12/?p=32793</remarks>
public static void SetDoubleBuffered(this Control control)
public static bool SetDoubleBuffered(this Control control)
{
// Double buffering can make DGV slow in remote desktop
if (!SystemInformation.TerminalServerSession)
if (SystemInformation.TerminalServerSession)
{
var dgvType = control.GetType();
var pi = dgvType.GetProperty("DoubleBuffered",
BindingFlags.Instance | BindingFlags.NonPublic);
pi.SetValue(control, true, null);
return false;
}
 
var dgvType = control.GetType();
var pi = dgvType.GetProperty("DoubleBuffered",
BindingFlags.Instance | BindingFlags.NonPublic);
if (pi == null)
{
return false;
}
 
pi.SetValue(control, true, null);
 
return true;
}
 
public static async Task<Icon> CreateIconFromResource(string resource)
{
var iconBytes = await LoadResource(resource);
using var iconMemoryStream = new MemoryStream(iconBytes);
var bitmap = (Bitmap)Image.FromStream(iconMemoryStream);
var bitmapIntPtr = bitmap.GetHicon();
var icon = Icon.FromHandle(bitmapIntPtr);
return icon;
using (var iconMemoryStream = new MemoryStream(iconBytes))
{
var bitmap = (Bitmap) Image.FromStream(iconMemoryStream);
var bitmapIntPtr = bitmap.GetHicon();
var icon = Icon.FromHandle(bitmapIntPtr);
return icon;
}
}
 
public static async Task<byte[]> LoadResource(string resource)
@@ -73,16 +98,21 @@
{
var assembly = Assembly.GetExecutingAssembly();
 
using var manifestResourceStream = assembly.GetManifestResourceStream(resource);
if (manifestResourceStream == null) return null;
using (var manifestResourceStream = assembly.GetManifestResourceStream(resource))
{
if (manifestResourceStream == null)
{
return null;
}
 
var memoryStream = new MemoryStream();
var memoryStream = new MemoryStream();
 
await manifestResourceStream.CopyToAsync(memoryStream);
await manifestResourceStream.CopyToAsync(memoryStream);
 
memoryStream.Position = 0L;
memoryStream.Position = 0L;
 
return memoryStream.ToArray();
return memoryStream.ToArray();
}
}
 
public static void InvokeIfRequired<T>(this T control, Action<T> action) where T : Control
@@ -89,7 +119,7 @@
{
if (control.InvokeRequired)
{
control.BeginInvoke((MethodInvoker)delegate { action(control); });
control.BeginInvoke((MethodInvoker) delegate { action(control); });
return;
}
 
@@ -96,65 +126,42 @@
action(control);
}
 
/// <summary>
/// Attempts to access a file within <see cref="timeout" /> milliseconds by retrying to access the file every
/// <see cref="retry" /> milliseconds.
/// </summary>
/// <param name="path">the path to the file</param>
/// <param name="mode">the file mode used to access the file</param>
/// <param name="access">the file access to use</param>
/// <param name="share">the file share to use</param>
/// <param name="cancellationToken">a cancellation token</param>
/// <param name="retry">the amount of milliseconds to retry between accesses to the file</param>
/// <param name="timeout">the amount of time in milliseconds to attempt and access the file</param>
/// <returns>a file stream if the file could be accessed within the allotted time</returns>
public static async Task<FileStream> GetFileStream(string path, FileMode mode, FileAccess access,
FileShare share, CancellationToken cancellationToken,
int retry = 1000, int timeout = 60000)
public static T MapValueToRange<T>(this T value, T xMin, T xMax, T yMin, T yMax)
where T : struct, IComparable<T>, IConvertible
{
var time = Stopwatch.StartNew();
return (dynamic) yMin +
((dynamic) yMax - (dynamic) yMin) * ((dynamic) value - (dynamic) xMin) /
((dynamic) xMax - (dynamic) xMin);
}
 
while (time.ElapsedMilliseconds < timeout && !cancellationToken.IsCancellationRequested)
public static IEnumerable<TU> SequenceSubtract<TU, TV>(this IEnumerable<TU> a,
IEnumerable<TV> b,
Func<TU, TV, bool> cmp)
{
var eb = new List<TV>(b);
 
using (var ea = a.GetEnumerator())
{
try
while (ea.MoveNext())
{
return new FileStream(path, mode, access, share);
}
catch (IOException e)
{
// access error
if (e.HResult != -2147024864) throw;
}
if (ea.Current == null)
{
continue;
}
 
await Task.Delay(retry, cancellationToken);
}
foreach (var ib in eb)
{
if (cmp.Invoke(ea.Current, ib))
{
continue;
}
 
throw new TimeoutException($"Failed to get a access to {path} within {timeout}ms.");
}
yield return ea.Current;
 
/// <summary>
/// Returns the machine GUID.
/// </summary>
/// <returns>the GUID of the machine</returns>
/// <remarks>https://stackoverflow.com/questions/2333149/how-to-fast-get-hardware-id-in-c</remarks>
public static string GetMachineGuid()
{
var location = @"SOFTWARE\Microsoft\Cryptography";
var name = "MachineGuid";
 
using var localMachineX64View =
RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
using var rk = localMachineX64View.OpenSubKey(location);
if (rk == null)
throw new KeyNotFoundException(
$"Key Not Found: {location}");
 
var machineGuid = rk.GetValue(name);
if (machineGuid == null)
throw new IndexOutOfRangeException(
$"Index Not Found: {name}");
 
return machineGuid.ToString();
break;
}
}
}
}
 
#endregion