Skip to content

Instantly share code, notes, and snippets.

@LoriBru
LoriBru / MainVM.cs
Last active January 9, 2019 15:41
Change thread invariant culture to mantain a common separator for decimal numbers.
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
var ci = (CultureInfo)CultureInfo.CurrentCulture.Clone();
ci.NumberFormat.NumberDecimalSeparator = ".";
@LoriBru
LoriBru / MainVM.cs
Created January 9, 2019 15:38
Get a file from the project resources.
using (Stream stream = this.GetType().Assembly.GetManifestResourceStream("App.Resources." + "File.ext"))
{
if (stream == null)
{
throw new ArgumentException("No such resource", "File.ext");
}
using (Stream output = File.OpenWrite(currentAnalysisPath + $@"\Resources\File.ext"))
{
stream.CopyTo(output);
}
@LoriBru
LoriBru / MainVM.cs
Created January 9, 2019 15:45
Run a method within a task.
var task = Task.Run(() => LongMethodAsync());
@LoriBru
LoriBru / MainVm.cs
Created January 9, 2019 15:46
Copy all files from a directory to another.
var root = new DirectoryInfo(rootPath);
foreach (var file in root.GetFiles())
{
File.Copy(file.FullName, "DestinationFolder\" + file.Name, true);
}
@LoriBru
LoriBru / MainVM.cs
Created January 14, 2019 10:44
Clone a directory with all its files.
public static void CloneDirectory(string root, string dest)
{
foreach (var directory in Directory.GetDirectories(root))
{
var dirName = Path.GetFileName(directory);
if (!Directory.Exists(Path.Combine(dest, dirName)))
{
Directory.CreateDirectory(Path.Combine(dest, dirName));
}
CloneDirectory(directory, Path.Combine(dest, dirName));
@LoriBru
LoriBru / MainVM.cs
Last active January 14, 2019 10:49
This method converts a hex string to a byte array.
public static byte[] StringToByteArray(string hex)
{
return Enumerable.Range(0, hex.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();
}
@LoriBru
LoriBru / MainVM.cs
Created January 14, 2019 11:16
Full property with OnpropertyChanged
string name;
public string Name
{
get { return name; }
set
{
name = value;
OnPropertyChanged(nameof(Name));
}
@LoriBru
LoriBru / RelayCommand.cs
Created January 14, 2019 11:18
RelayCommand class
/// <summary>
/// Interface for commands to use in MVVM Pattern. It implements ICommand interface.
/// </summary>
public class RelayCommand : ICommand
{
readonly Action<object> execute;
readonly Func<object, bool> canExecute;
/// <summary>
/// CanExecuteChanged event handler.
@LoriBru
LoriBru / MainVM.cs
Created January 14, 2019 11:18
Setting shutdown mode to OnMainWindowClose so that all pending tasks will terminate when closing the main window.
Application.Current.ShutdownMode = ShutdownMode.OnMainWindowClose;
@LoriBru
LoriBru / RelayCommand.cs
Created January 14, 2019 11:19
Assign a RelayCommand to a method.
OneCommand = new RelayCommand(o => OneMethod(), o => true);