We all had the issue with an OrderBy on a collection of String, having that kind of result: Test1 Test10 Test2 Test20 Test3 Instead of: Test1 Test2 Test3 Test10 Test20 Here is the solution: public class NaturalComparer : Comparer<string> { public override int Compare(string x, string y) { if (x == y) return 0; string[] […]
C# – DistinctBy extension
Hello devz, The Distinct extension from System.Linq is really useful but the DistinctBy a property is missing. So we will create our own DistinctBy extension. No worries, it’s really easy. What is DistinctBy? DistinctBy is a very simple extension snippet for C# which allows the developer to remove duplicated objects in a collection based on […]
C# – Verify if it’s a network path
Hello, An easy way to determine if the path is a network path: [DllImport(“shlwapi.dll”)] public static extern bool PathIsNetworkPath(string pszPath); Yes, that’s all… Bye bye
C# – Check Write Access To Folder
Hi, An easy way to know if the user have write access to a folder can be done this way: public static bool HasWriteAccessToFolder(string folderPath) { try { // Attempt to get a list of security permissions from the folder. // This will raise an exception if the path is read only or do not […]
C# – Timer Elapsed
Time is money! But how do you manage the time in code? The simplest way of doing it is by using a Timer (System.Timers): using System; using System.Timers; namespace TimerEvent { class Program { static void Main(string[] args) { var keyPressed = false; const int delay = 1000; //Define the delay for the Timer var […]
C# – Determine the available space on a network drive
Hi peeps, Today at work I had an issue with a shared drive on the network which has a quota. So I was wondering how to know if the allowed space is already full or not. According to my readings on Google, there are only two methods that works for a network drive: WMI or […]
C# – Coma and Dot decimal separator
Hi peeps, You probably know the common issue with the decimal seprator. Depending on the country, the keyboard or what your client wants (which can be variable…), the separator will be a coma or a dot (or even something else…). In my case, the Culture is “en-US“, on a Belgian keyboard where the decimal separator […]
C# – Store Variables and Collections in Config file
In this article, we will talk about the config file used for a C# application. What is the goal of it, and how to use it. How to get values and create your own config section. What is a config file? It happens sometimes (quite often actually) that we need to store some parameters, default […]
WPF – Change color of a row in a DataGrid depending on the value with DataTrigger
Hi folks, Today we will see how to change the color or a row depending on the value of the content. To do that, create a simple View: <Window x:Class=”DataTrigger_Test.MainWindow” xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation” xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml” xmlns:d=”http://schemas.microsoft.com/expression/blend/2008″ xmlns:mc=”http://schemas.openxmlformats.org/markup-compatibility/2006″ mc:Ignorable=”d” Title=”MainWindow” Height=”350″ Width=”525″> <Grid> <DataGrid Margin=”10″ ItemsSource=”{Binding Users}” AutoGenerateColumns=”False” ColumnWidth=”*” HorizontalAlignment=”Stretch” VerticalAlignment=”Stretch” HorizontalContentAlignment=”Stretch” EnableRowVirtualization=”false” EnableColumnVirtualization=”false” CanUserAddRows=”False” CanUserReorderColumns=”False” CanUserResizeColumns=”True”> <DataGrid.CellStyle> <Style […]
Xamarin.Forms – Use built-in Dependency Injection (IoC)
Hello Devz, Xamarin.Forms have its own Dependency Injection implementation. Of course you can still use other ones like MvvmCross, Ninject and so on, but they will ask you a bit more configuration and changes in your code. The one from Xamarin.Forms works pretty well and is really simple to use. Here is a simple example. […]