How to get current Windows 10 Theme? In the namespace Windows.UI.ViewManagement, you can Access to the UISettings class. It will allow you to get the Window background and deduce the current theme. How to set the theme when multiple views are open? You must iterate all the views via the CoreApplication. Secondly, you can go […]
C# – JSON serialize and deserialize
Hello Devz, JSON files are really common, we can see them everywhere. They provide a good structure to organize data, are simpler and lighter than XML files, and still human readable. In this simple example, we will show you how to use the Json.NET library (Newtonsoft) from NuGet. It is pretty simple to serialize and […]
C# – Copy a folder, its content and the sub-directories
Hello Devz, It’s unbelievable but yes, the System.IO provided by .NET doesn’t have a simple method to copy a folder, its content and the sub directories to another destination. There are a few different techniques to copy a directory with C#, using the command XCopy, or using a recursive method. The best one I saw […]
C# – Simple email validation regex
Hello Devz, Do you have a web or WPF application that needs email address validation? Regex (regular expression) can help you. This post outlines a simple email validation regex that you can use straight away. What is a regex? A regular expression (regex for short) describes a search pattern using a particular text string. It’s […]
C# – Natural Comparer
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | public class NaturalComparer : Comparer<string> { public override int Compare(string x, string y) { if (x == y) return 0; string[] x1, y1; x1 = Regex.Split(x.Replace(" ", ""), "([0-9]+)"); y1 = Regex.Split(y.Replace(" ", ""), "([0-9]+)"); for (int i = 0; i < x1.Length && i < y1.Length; i++) if (x1[i] != y1[i]) return PartCompare(x1[i], y1[i]); if (y1.Length > x1.Length) return 1; else if (x1.Length > y1.Length) return -1; else return 0; } private static int PartCompare(string left, string right) { int x, y; if (int.TryParse(left, out x) && int.TryParse(right, out y)) return x.CompareTo(y); return left.CompareTo(right); } } public class ReverseNaturalComparer : NaturalComparer { public override int Compare(string x, string y) { return base.Compare(y, x); } } |
And this is how to use it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | [TestClass] public class NaturalComparer_Test { public List<string> _unsortedCollection { get { var stringCollection = new List<string>(); stringCollection.Add("Test10"); stringCollection.Add("Test3"); stringCollection.Add("Test1"); stringCollection.Add("Test20"); stringCollection.Add("Test2"); return stringCollection; } private set { } } [TestMethod] public void NaturalSort_Test() { var sortedCollection = _unsortedCollection .OrderBy(x => x, new NaturalComparer()) .ToList(); Assert.IsNotNull(sortedCollection); Assert.IsTrue(sortedCollection.Last() == "Test20"); } [TestMethod] public void ReversNaturalSort_Test() { var sortedCollection = _unsortedCollection .OrderBy(x => x, new ReverseNaturalComparer()) .ToList(); Assert.IsNotNull(sortedCollection); Assert.IsTrue(sortedCollection.Last() == "Test1"); } } |
Happy coding! 😉
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:
1 2 | [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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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 have access to view the permissions. System.Security.AccessControl.DirectorySecurity ds = Directory.GetAccessControl(folderPath); return true; } catch (UnauthorizedAccessException) { return false; } } |
Happy coding! 🙂
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):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | 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 timer = new Timer(delay); //Create the Timer timer.Elapsed += Timer_Elapsed; //Set the Timer event timer.Enabled = true; //Start the Timer Console.WriteLine("Press any key to stop..."); while (!keyPressed) { var key = Console.ReadKey(); if (key != null) keyPressed = true; } } private static void Timer_Elapsed(object sender, ElapsedEventArgs e) { //Do something when we reached the Delay of the Timer Console.Write($"\r{DateTime.Now}"); // \r to rewrite on the same line } } } |
Enjoy!  🙂
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 […]