This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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 = "_") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using System.Text.RegularExpressions; | |
using System.Text; | |
string str = @" | |
a1 = b1; | |
a2 = b2; | |
a3 = b3;"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using static System.Diagnostics.Debug; | |
public static class Program | |
{ | |
private enum Demographic | |
{ | |
Men18To30, | |
CollegeEducatedWomenOver40, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void EreaseSelection(this ICollectionView collectionView) => collectionView.MoveCurrentToPosition(-1); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<TextBlock> | |
<TextBlock.Text> | |
<MultiBinding StringFormat="{}{0:F0}x{1:F0}"> | |
<Binding ElementName="window" Path="ActualWidth" /> | |
<Binding ElementName="window" Path="ActualHeight" /> | |
</MultiBinding> | |
</TextBlock.Text> | |
</TextBlock> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static void EnsureAdminPrivileges() | |
{ | |
if (new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator)) | |
{ | |
return; | |
} | |
ProcessStartInfo processInfo = new(Environment.ProcessPath.NotNull()) | |
{ | |
UseShellExecute = true, | |
Verb = "runas" |
OlderNewer