Disabling SMBv1 on Windows Server 2019
SMBv1 (client and server)
Detect:
Get-WindowsOptionalFeature -Online -FeatureName smb1protocol
Disable:
Disable-WindowsOptionalFeature -Online -FeatureName smb1protocol
Enable:
Enable-WindowsOptionalFeature -Online -FeatureName smb1protocol
Convert Color Code into Brush in WPF
var converter = new System.Windows.Media.BrushConverter(); var brush = (Brush)converter.ConvertFromString("#FFFFFF90"); Fill = brush;
References
https://stackoverflow.com/questions/6808739/how-to-convert-color-code-into-media-brush
Check if a file contains a specific line/string in Bash
if grep -q "$STRING" "$FILE" ; then echo 'the string exists' ; else echo 'the string does not exist' ; fi
References
https://unix.stackexchange.com/questions/530561/shell-script-check-if-a-file-contains-a-specific-line-string
https://stackoverflow.com/questions/10552711/how-to-make-if-not-true-condition
Allow Incoming Connection from Specific IP Address or Subnet in UFW
Allow Incoming SSH from Specific IP Address or Subnet
sudo ufw allow from 15.15.15.0/24 to any port 22
sudo ufw allow from 15.15.15.65 to any port 22
Allow Incoming Rsync from Specific IP Address or Subnet
sudo ufw allow from 15.15.15.0/24 to any port 873
Allow All Incoming HTTP and HTTPS
sudo ufw allow proto tcp from any to any port 80,443
Allow MySQL from Specific IP Address or Subnet
sudo ufw allow from 15.15.15.0/24 to any port 3306
Allow MySQL to Specific Network Interface
sudo ufw allow in on eth1 to any port 3306
References
https://www.digitalocean.com/community/tutorials/ufw-essentials-common-firewall-rules-and-commands
Use await in Class Constructor in C#
use a static async method that returns a class instance created by a private constructor
public class ViewModel { public ObservableCollection<TData> Data { get; set; } //static async method that behave like a constructor async public static Task<ViewModel> BuildViewModelAsync() { ObservableCollection<TData> tmpData = await GetDataTask(); return new ViewModel(tmpData); } // private constructor called by the async method private ViewModel(ObservableCollection<TData> Data) { this.Data = Data; } }
References
https://stackoverflow.com/questions/8145479/can-constructors-be-async
Switching back to the UI thread in WPF
Dispatcher.Invoke(() => { // Set property or change UI compomponents. });
Dispatcher.BeginInvoke(new Action(UpdateControls));
References
https://stephenhaunts.com/2015/06/12/update-a-wpf-ui-from-another-thread/
https://stackoverflow.com/questions/19009174/dispatcher-invoke-vs-begininvoke-confusion/
https://medium.com/criteo-engineering/switching-back-to-the-ui-thread-in-wpf-uwp-in-modern-c-5dc1cc8efa5e
Implementing the Singleton Pattern in C#
public sealed class EventBus { private static readonly EventBus instance = new EventBus(); // Explicit static constructor to tell C# compiler // not to mark type as beforefieldinit static EventBus() { } private EventBus() { } public static EventBus Instance { get { return instance; } } }
Using .NET 4’s Lazy<T> type
public sealed class Singleton5 { private Singleton5() { } private static readonly Lazy<Singleton5> lazy = new Lazy<Singleton5>(() => new Singleton5()); public static Singleton5 Instance { get { return lazy.Value; } } }
References
https://csharpindepth.com/articles/singleton
https://www.c-sharpcorner.com/UploadFile/8911c4/singleton-design-pattern-in-C-Sharp/
https://riptutorial.com/csharp/example/6795/lazy–thread-safe-singleton–using-lazy-t–
Value conversion with IValueConverter in WPF
<Window x:Class="WpfTutorialSamples.DataBinding.ConverterSample" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfTutorialSamples.DataBinding" Title="ConverterSample" Height="140" Width="250"> <Window.Resources> <local:YesNoToBooleanConverter x:Key="YesNoToBooleanConverter" /> </Window.Resources> <StackPanel Margin="10"> <TextBox Name="txtValue" /> <WrapPanel Margin="0,10"> <TextBlock Text="Current value is: " /> <TextBlock Text="{Binding ElementName=txtValue, Path=Text, Converter={StaticResource YesNoToBooleanConverter}}"></TextBlock> </WrapPanel> <CheckBox IsChecked="{Binding ElementName=txtValue, Path=Text, Converter={StaticResource YesNoToBooleanConverter}}" Content="Yes" /> </StackPanel> </Window>
using System; using System.Windows; using System.Windows.Data; namespace WpfTutorialSamples.DataBinding { public partial class ConverterSample : Window { public ConverterSample() { InitializeComponent(); } } public class YesNoToBooleanConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { switch(value.ToString().ToLower()) { case "yes": case "oui": return true; case "no": case "non": return false; } return false; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if(value is bool) { if((bool)value == true) return "yes"; else return "no"; } return "no"; } } }
References
https://wpf-tutorial.com/data-binding/value-conversion-with-ivalueconverter/