Skip to content

Instantly share code, notes, and snippets.

View SamKr's full-sized avatar
🤸
Tinkering

Sam SamKr

🤸
Tinkering
  • Netherlands
  • 03:11 (UTC +01:00)
View GitHub Profile
@SamKr
SamKr / RunOnce.cs
Last active January 6, 2020 14:02
Adds the current exec to runonce
const string userRoot = "HKEY_CURRENT_USER";
const string subkey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce";
const string keyName = userRoot + "\\" + subkey;
var location = new Uri(Assembly.GetEntryAssembly().GetName().CodeBase);
var fileInfo = new FileInfo(location.AbsolutePath);
var fileLoc = fileInfo.FullName;
var fileDesc = Path.GetFileNameWithoutExtension(fileInfo.Name);
Registry.SetValue(keyName, fileDesc, fileLoc, RegistryValueKind.String);
@SamKr
SamKr / Monitor.cs
Created July 3, 2019 08:51
Use perf counters to monitor a process' CPU/mem usage
using System.Diagnostics;
var counters = new List<PerformanceCounter>();
using (var proc = Process.GetCurrentProcess())
{
var processorTimeCounter = new PerformanceCounter("Process", "% Processor Time", proc.ProcessName);
processorTimeCounter.NextValue();
counters.Add(processorTimeCounter);
@SamKr
SamKr / ProcessOwner.cs
Last active June 27, 2019 09:22
Get the owner of a process, without using slow WMI calls
[DllImport("advapi32.dll", SetLastError = true)]
private static extern bool OpenProcessToken(IntPtr ProcessHandle, uint DesiredAccess, out IntPtr TokenHandle);
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool CloseHandle(IntPtr hObject);
internal static string GetProcessUser(Process process, bool includeDomain = false)
{
var processHandle = IntPtr.Zero;
try
@SamKr
SamKr / InvokeReturnValue.cs
Last active June 27, 2019 09:22
Invoke a method (if GUI hook is required) and get its return value
if (object.InvokeRequired) return (bool)object.Invoke(new Func<bool>(() => FunctionName(optionalParameter)));
@SamKr
SamKr / HdLogo.cs
Created April 5, 2019 09:00
Creates the HDServices logo as ASCII for console applications
internal class HdLogo
{
private static string _hdLogo = "";
internal static string GetLogo()
{
if (string.IsNullOrEmpty(_hdLogo)) Initialise();
return _hdLogo;
}
@SamKr
SamKr / UppercaseWords.cs
Created March 12, 2019 10:15
Change the first letter of every word into uppercase, lowercasing everything else
internal static string UppercaseWords(string value)
{
if (string.IsNullOrEmpty(value)) return value;
value = value.ToLower();
var array = value.ToCharArray();
if (array.Length >= 1)
{
if (char.IsLower(array[0]))
{
@SamKr
SamKr / IterateString.cs
Last active March 12, 2019 10:15
Iterate through multiline string, checking for null
if (!string.IsNullOrEmpty(multiLineString))
{
using (var reader = new StringReader(multiLineString))
{
string line;
while ((line = reader.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
@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;
@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 / 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;
}