/trunk/Winify/Utilities/Miscellaneous.cs |
@@ -17,32 +17,28 @@ |
|
public static bool LaunchOnBootSet(bool enable) |
{ |
using (var key = Registry.CurrentUser.OpenSubKey |
("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true)) |
using var key = Registry.CurrentUser.OpenSubKey |
("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); |
if (key == null) return false; |
|
switch (enable) |
{ |
if (key == null) return false; |
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; |
} |
return true; |
} |
|
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> |
@@ -66,13 +62,11 @@ |
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) |
@@ -79,18 +73,16 @@ |
{ |
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 |
@@ -150,23 +142,19 @@ |
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( |
string.Format("Key Not Found: {0}", location)); |
using var localMachineX64View = |
RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64); |
using var rk = localMachineX64View.OpenSubKey(location); |
if (rk == null) |
throw new KeyNotFoundException( |
string.Format("Key Not Found: {0}", location)); |
|
var machineGuid = rk.GetValue(name); |
if (machineGuid == null) |
throw new IndexOutOfRangeException( |
string.Format("Index Not Found: {0}", name)); |
var machineGuid = rk.GetValue(name); |
if (machineGuid == null) |
throw new IndexOutOfRangeException( |
string.Format("Index Not Found: {0}", name)); |
|
return machineGuid.ToString(); |
} |
} |
return machineGuid.ToString(); |
} |
|
#endregion |