Skip to content

Instantly share code, notes, and snippets.

View SamKr's full-sized avatar
🤸
Tinkering

Sam SamKr

🤸
Tinkering
  • Netherlands
  • 22:55 (UTC +01:00)
View GitHub Profile
internal static string GetQuotedValue(string value)
{
try
{
if (!value.Contains("\"")) return string.Empty;
var values = from Match match in Regex.Matches(value, "\"([^\"]*)\"") select match.ToString();
var resultArray = values as string[] ?? values.ToArray();
return !resultArray.Any() ? string.Empty : resultArray.First().Replace("\"", "");
@SamKr
SamKr / HideWindow.cs
Created February 18, 2020 09:26
Hide Borderless Window
protected override CreateParams CreateParams
{
get
{
var cp = base.CreateParams;
// turn on WS_EX_TOOLWINDOW style bit
cp.ExStyle |= 0x80;
return cp;
}
}
@SamKr
SamKr / SeriLog.cs
Created January 29, 2020 15:12
SeriLog logger
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.Console()
.WriteTo.File("logfile.log", rollingInterval: RollingInterval.Day, restrictedToMinimumLevel: LogEventLevel.Information)
.WriteTo.CoderrSeriLogSink()
.CreateLogger();
public class CoderrSeriLogSink : ILogEventSink
@SamKr
SamKr / PasswordGenerator.cs
Created January 20, 2020 15:39
Password Generator
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using System.Security.Cryptography;
using System.Text;
public static class PasswordGenerator
{
public static string Generate(int length)
@SamKr
SamKr / DeleteFolder.cs
Created January 9, 2020 10:02
Recursively delete folder
internal static void DeleteFolderRecursively(string rootMap)
{
var created = DateTime.MinValue;
try
{
created = Directory.GetCreationTime(rootMap);
foreach (var map in Directory.GetDirectories(rootMap))
{
@SamKr
SamKr / DeleteAllFilesInFolder.cs
Created January 9, 2020 10:01
Delete all files in folder
public static void DeleteAllFilesInFolder(string path)
{
try
{
if (!Directory.Exists(path)) return;
var di = new DirectoryInfo(path);
foreach (var file in di.GetFiles())
{
try
@SamKr
SamKr / IsRunning.cs
Last active January 6, 2020 14:02
Checks whether the current exec is already running (user agnostic)
public static bool IsRunning()
{
var proc = Assembly.GetExecutingAssembly().GetName().Name;
var processes = Process.GetProcesses().ToList();
var activeProcs = processes.Where(x => x.ProcessName.ToLower() == proc).ToList();
return activeProcs.Count > 1;
}
@SamKr
SamKr / LocalUser.cs
Last active January 6, 2020 14:02
Search for the existance of a local user
using System.DirectoryServices.AccountManagement;
using (var pc = new PrincipalContext(ContextType.Machine))
{
var up = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, "gebruiker2");
var userExists = (up != null);
if (userExists) Console.WriteLine("user exists");
else Console.WriteLine("user not found");
}
@SamKr
SamKr / CheckInternet.cs
Last active January 6, 2020 14:03
Checks for an active internet connection
internal static bool CheckForInternetConnection()
{
try
{
using (var client = new WebClient())
using (client.OpenRead("http://google.com/generate_204"))
{
return true;
}
}
@SamKr
SamKr / ActiveIp.cs
Last active January 6, 2020 14:03
Gets the current active IP address
using (var s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, System.Net.Sockets.ProtocolType.Udp))
{
s.Bind(new IPEndPoint(IPAddress.Any, 0));
s.Connect("google.com", 0);
var ipaddr = s.LocalEndPoint as IPEndPoint;
var addr = ipaddr?.Address?.ToString() ?? "";
Console.WriteLine(addr);
}