Skip to content

Instantly share code, notes, and snippets.

View 5cover's full-sized avatar

Scover 5cover

  • France
  • 17:28 (UTC +02:00)
View GitHub Profile
@5cover
5cover / FilenameHelpers.cs
Last active November 19, 2022 19:17
Create a valid NTFS Windows filename out of a string
/// <summary>Creates a valid Windows filename from a string.</summary>
/// <param name="filename">The filename candidate.</param>
/// <param name="replaceInvalidCharsWith">What to replace invalid filename chars in <paramref name="filename"/> with.</param>
/// <returns>
/// A new <see cref="string"/>, equivalent to <paramref name="filename"/>, but modified to be a valid Windows filename if it
/// <paramref name="filename"/> wasn't already.
/// </returns>
/// <exception cref="ArgumentException"/>
/// <remarks>The length of the filename is not checked, and the casing is not modified.</remarks>
public static string ToFilename(this string filename, string replaceInvalidCharsWith = "_")
@5cover
5cover / Helpers.cs
Last active May 1, 2023 15:36
C# Tuple assignement Regex
using System;
using System.Linq;
using System.Text.RegularExpressions;
using System.Text;
string str = @"
a1 = b1;
a2 = b2;
a3 = b3;";
@5cover
5cover / Redesign your C# Logo - Lemon Demon.cs
Last active May 1, 2023 15:34
Syntactically valid C# program based on the the Lemon Demon song "Redesign your Logo"
using System;
using System.Collections.Generic;
using static System.Diagnostics.Debug;
public static class Program
{
private enum Demographic
{
Men18To30,
CollegeEducatedWomenOver40,
@5cover
5cover / Extensions.cs
Created May 1, 2023 16:57
FindVisualChildren C# WPF
public static IEnumerable<T> FindVisualChildren<T>(this DependencyObject depObj) where T : DependencyObject
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
var child = VisualTreeHelper.GetChild(depObj, i);
if (child is T t)
{
yield return t;
}
foreach (T childOfChild in FindVisualChildren<T>(child))
@5cover
5cover / Ref.cs
Created May 15, 2023 18:07
C# Reference wrapper
public sealed class Ref<T>
{
public Ref(T value) => Value = value;
public T Value { get; set; }
public static implicit operator T(Ref<T> @ref) => @ref.Value;
}
@5cover
5cover / EqualsConverter.cs
Created June 1, 2023 18:44
Converter used to efficiently deal with radio buttons
using System.Globalization;
using System.Windows.Data;
/// <summary>A converter based on equality comparisons between the value and the parameter.</summary>
public sealed class EqualsConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
=> Equals(value, parameter);
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
@5cover
5cover / Extensions.cs
Created June 3, 2023 21:06
Erease selection of an ICollectionView
public static void EreaseSelection(this ICollectionView collectionView) => collectionView.MoveCurrentToPosition(-1);
@5cover
5cover / Window.xaml
Created June 4, 2023 15:24
TextBlock bound to current size of window.
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0:F0}x{1:F0}">
<Binding ElementName="window" Path="ActualWidth" />
<Binding ElementName="window" Path="ActualHeight" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
@5cover
5cover / Extensions.cs
Created June 6, 2023 21:02
C# Task WithTimeout() extension method
public static async Task WithTimeout(this Task task, TimeSpan timeout)
{
CancellationTokenSource cts = new();
if (task == await Task.WhenAny(task, Task.Delay(timeout, cts.Token)))
{
cts.Cancel();
}
else
{
throw new TimeoutException();
@5cover
5cover / Helpers.cs
Created June 9, 2023 18:03
Ensure admin privileges helper method. Restarts the application with admin privileges.
private static void EnsureAdminPrivileges()
{
if (new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator))
{
return;
}
ProcessStartInfo processInfo = new(Environment.ProcessPath.NotNull())
{
UseShellExecute = true,
Verb = "runas"