Get All Files and Sub-Files with Their Size in C#

Nizamuddin Siddiqui
Updated on 25-Nov-2020 12:05:22

1K+ Views

To get the files, C# provides a method Directory.GetFilesDirectory.GetFiles returns the names of all the files (including their paths) that match the specified search pattern, and optionally searches subdirectories.In the below example * is matches Zero or more characters in that position.SearchOption TopDirectoryOnly. Searches only the top directoriesSearchOption AllDirectories .Searches all the top directories and sub directoriesFileInfo gets the file information like Length, nameExample 1static void Main (string[] args) {    string rootPath = @"C:\Users\Koushik\Desktop\TestFolder";    var files = Directory.GetFiles(rootPath, "*.*", SearchOption.AllDirectories);    foreach (string file in files) {       Console.WriteLine(file);    }    Console.ReadLine (); }OutputC:\Users\Koushik\Desktop\TestFolder\TestFolderMain\TestFolderMain.txt ... Read More

Sum Up a Number Until It Becomes 1 Digit in JavaScript

AmitDiwan
Updated on 25-Nov-2020 12:03:49

200 Views

We are required to write a JavaScript function that takes in a Number as the only input. The function should do one simple thing −keep adding the resultant digits until they converse to a single digit number.For example −const num = 5798;i.e.5 + 7 + 9 + 8 = 29 2 + 9 = 11 1 + 1 = 2Hence, the output should be 2ExampleThe code for this will be −const num = 5798; const sumDigits = (num, sum = 0) => {    if(num){       return sumDigits(Math.floor(num / 10), sum + (num % 10));    };    return sum; }; const repeatSum = (num) => {    if(num > 9){       return repeatSum(sumDigits(num));    };    return num; }; console.log(repeatSum(num));OutputAnd the output in the console will be −2

Implement IDisposable Design Pattern in C#

Nizamuddin Siddiqui
Updated on 25-Nov-2020 12:03:45

2K+ Views

We should use an IDisposable design pattern (or Dispose Pattern) when we need to dispose of unmanaged objects.For implementing the IDisposable design pattern, the class which deals with unmanaged objects directly or indirectly should implement the IDisposable interface.And implement the method Dispose declared inside of the IDisposable interface. We do not directly deal with unmanaged objects. But we deal with managed classes, which deals directly with unmanaged objects. For example, File handlers, connection string, HTTP streams, etc.Important aspect of this pattern is that it makes easier for inherited classes to follow the IDisposable design pattern. And it is because of ... Read More

Implement Null Object Pattern in C#

Nizamuddin Siddiqui
Updated on 25-Nov-2020 12:00:25

174 Views

The null object pattern helps us to write a clean code avoiding null checks where ever possible. Using the null object pattern, the callers do not have to care whether they have a null object or a real object. It is not possible to implement null object pattern in every scenario. Sometimes, it is likely to return a null reference and perform some null checks.Examplestatic class Program{    static void Main(string[] args){       Console.ReadLine();    }    public static IShape GetMobileByName(string mobileName){       IShape mobile = NullShape.Instance;       switch (mobileName){         ... Read More

What is Connection Pooling in C# and How to Achieve It

Nizamuddin Siddiqui
Updated on 25-Nov-2020 11:56:02

869 Views

These are used to import namespaces (or create aliases for namespaces or types).These go at the top of the file, before any declarations.using System; using System.IO; using WinForms = global::System.Windows.Forms; using WinButton = WinForms::Button;The using statement ensures that Dispose() is called even if an exception occurs when you are creating objects and calling methods, properties and so on. Dispose() is a method that is present in the IDisposable interface that helps to implement custom Garbage Collection. In other words, if we are doing some database operation (Insert, Update, Delete) but somehow an exception occurs, then here the using statement closes ... Read More

Get Human Readable File Size in Bytes Abbreviation using C#

Nizamuddin Siddiqui
Updated on 25-Nov-2020 11:54:27

486 Views

To get the directories C# provides a method Directory.GetDirectoriesDirectory.GetDirectories returns the names of the subdirectories (including their paths) that match the specified search pattern in the specified directory, and optionally searches subdirectories.To get the files, C# provides a method Directory.GetFilesDirectory.GetFiles returns the names of all the files (including their paths) that match the specified search pattern, and optionally searches subdirectoriesTo get the file length , C# provides a property LengthExamplestatic void Main(string[] args) {    string rootPath = @"C:\Users\Koushik\Desktop\TestFolder";    var files = Directory.GetFiles(rootPath, "*.*", SearchOption.AllDirectories);    foreach (string file in files) {       long size = ... Read More

Catch Exception Thrown by Async Void Method in C#

Nizamuddin Siddiqui
Updated on 25-Nov-2020 11:53:15

903 Views

In synchronous C# code, the exceptions are propagated up the call stack until they reach an appropriate catch block that can handle the exception. However, exception handling in asynchronous methods is not as straightforward.An asynchronous method in C# can have three types of return value: void, Task, and Task. When an exception occurs in an async method that has a return type of Task or Task, the exception object is wrapped in an instance of AggregateException and attached to the Task object.If multiple exceptions are thrown, all of them are stored in the Task object.Example 1static async Task Main(string[] args) ... Read More

Get All Directories and Sub-Directories Inside a Path in C#

Nizamuddin Siddiqui
Updated on 25-Nov-2020 11:51:51

26K+ Views

To get the directories C# provides a method Directory.GetDirectories. The Directory.GetDirectories method returns the names of the subdirectories (including their paths) that match the specified search pattern in the specified directory, and optionally searches subdirectories.In the below example * is matches Zero or more characters in that position. SearchOption TopDirectoryOnly .Gets only the top directories and SearchOption AllDirectories .Gets all the top directories and sub directories.Note: The rootPath will be your systems rootPath so create a testfolder and use the rootPath accoridingly.Example 1static void Main (string[] args) {    string rootPath = @"C:\Users\Koushik\Desktop\TestFolder";    string[] dirs = Directory.GetDirectories(rootPath, "*", SearchOption.TopDirectoryOnly); ... Read More

Convert IEnumerable to List and Back to IEnumerable in C#

Nizamuddin Siddiqui
Updated on 25-Nov-2020 11:49:42

9K+ Views

IEnumerable is an interface defining a single method GetEnumerator() that returns an IEnumerator interface. It is the base interface for all non-generic collections that can be enumerated.This works for read-only access to a collection that implements that IEnumerable can be used with a foreach statement.List class represents the list of objects which can be accessed by index. It comes under the System.Collection.Generic namespace.List class can be used to create a collection of different types like integers, strings etc. List class also provides the methods to search, sort, and manipulate lists.Example 1static void Main(string[] args) {    List list = new ... Read More

Check MinLength and MaxLength Validation in C# Using Fluent Validation

Nizamuddin Siddiqui
Updated on 25-Nov-2020 11:46:46

2K+ Views

MaxLength ValidatorEnsures that the length of a particular string property is no longer than the specified value.Only valid on string propertiesString format args:{PropertyName} = The name of the property being validated{MaxLength} = Maximum length{TotalLength} = Number of characters entered{PropertyValue} = The current value of the propertyMinLength ValidatorEnsures that the length of a particular string property is longer than the specified value.Only valid on string properties{PropertyName} = The name of the property being validated{MinLength} = Minimum length{TotalLength} = Number of characters entered{PropertyValue} = The current value of the propertyExamplestatic void Main(string[] args){    List errors = new List();    PersonModel ... Read More

Advertisements