Skip to content

Instantly share code, notes, and snippets.

View SamKr's full-sized avatar
🤸
Tinkering

Sam SamKr

🤸
Tinkering
  • Netherlands
  • 01:52 (UTC +01:00)
View GitHub Profile
@SamKr
SamKr / KeyboardHook.cs
Created February 28, 2018 09:53
Captures global keypresses.
class GlobalKeyboardHookEventArgs : HandledEventArgs
{
public GlobalKeyboardHook.KeyboardState KeyboardState { get; private set; }
public GlobalKeyboardHook.LowLevelKeyboardInputEvent KeyboardData { get; private set; }
public GlobalKeyboardHookEventArgs(
GlobalKeyboardHook.LowLevelKeyboardInputEvent keyboardData,
GlobalKeyboardHook.KeyboardState keyboardState)
{
KeyboardData = keyboardData;
var start = new TimeSpan(2, 45, 0);
var end = new TimeSpan(4, 0, 0);
var now = DateTime.Now.TimeOfDay;
if ((now > start) && (now < end))
{
return;
}
@SamKr
SamKr / Log.cs
Last active October 24, 2018 16:01
Log text to file
if (!Directory.Exists(Variables.LogDirectory)) Directory.CreateDirectory(Variables.LogDirectory);
var logFile = $"[{DateTime.Now:yyyy-MM-dd}] Log.txt";
if (error) logFile = $"[{DateTime.Now:yyyy-MM-dd}] ErrorLog.txt";
logFile = Path.Combine(Variables.LogDirectory, logFile);
using (var stream = new FileStream(logFile, FileMode.Append, FileAccess.Write))
using (var writer = new StreamWriter(stream))
{
@SamKr
SamKr / DecodeUrl.cs
Created October 24, 2018 09:42
Decodes an HTTP URL
string newUrl;
var i = 0;
while ((newUrl = Uri.UnescapeDataString(url)) != url)
{
url = newUrl;
i++;
if (i > 1000) break;
}
@SamKr
SamKr / Powershell.cs
Created October 31, 2018 10:58
Launches a Powershell command with parameters
using (var pInstance = PowerShell.Create())
{
pInstance.AddCommand("Start-Process");
pInstance.AddParameter("-WindowStyle", "hidden");
pInstance.AddParameter("-FilePath", "\"explorer.exe\"");
pInstance.AddParameter("-ArgumentList", $"\"{drive}\"");
pInstance.Invoke();
}
@SamKr
SamKr / GetDistance.cs
Created November 2, 2018 10:16
Calculates distance between 2 geopoints
using System.Device.Location;
internal static double GetDistance(double lat, double lon)
{
var sCoord = new GeoCoordinate(Variables.HomeLat, Variables.HomeLon);
var eCoord = new GeoCoordinate(lat, lon);
return sCoord.GetDistanceTo(eCoord);
}
@SamKr
SamKr / LvDoubleBuff.cs
Created December 21, 2018 08:29
Prevents flickering when updating.
LvServers
.GetType()
.GetProperty("DoubleBuffered", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)
?.SetValue(LvServers, true, null);
@SamKr
SamKr / GetFirstMonday.cs
Created January 8, 2019 10:37
Get first monday of given year/month
internal static DateTime GetFirstMonday(int month, int year)
{
var dt = new DateTime(year, month, 1, 12, 00, 00);
for (var i = 0; i < 7; i++)
{
if (dt.DayOfWeek == DayOfWeek.Monday)
{
return dt;
}
@SamKr
SamKr / Log.cs
Last active June 27, 2019 09:23
Extensive console (+ file) logger, using a queue and color-coding
using System;
using System.Collections.Concurrent;
using System.ComponentModel;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using System.Threading;
using Coderr.Client;
namespace HDSupportIdentifier.Functions
@SamKr
SamKr / ExcelReader.cs
Last active March 12, 2019 10:15
Reads an XML file
// Microsoft Excel object in references-> COM tab
using Excel = Microsoft.Office.Interop.Excel;
var xlApp = new Excel.Application();
var xlWorkbook = xlApp.Workbooks.Open(Program.XmlFile);
Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
var xlRange = xlWorksheet.UsedRange;
var rowCount = xlRange.Rows.Count;
var colCount = xlRange.Columns.Count;