Monthly Archives: May 2021

C# || How To Get The Date Value & Time Value From A Date Using C#

The following is a module with functions which demonstrates how to get the date value and time value from a DateTime using C#.

The functions listed on this page demonstrates how to isolate the date portion of a date, as well as the time portion of a date. For example, given the date “5/23/2019 7:28:31 PM”, the date value would be 5/23/2019, and time value 7:28:31 PM.


1. Date Value

The example below demonstrates the use of ‘Utils.Methods.DateValue‘ to get the date value of a date.

The DateTime object value contains the date information, with the time portion set to midnight (00:00:00)


2. Time Value

The example below demonstrates the use of ‘Utils.Methods.TimeValue‘ to get the date value of a date.

The DateTime object contains the time information, with the date portion set to January 1 of the year 1


3. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


4. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

C# || How To Create Multiple Tasks With Maximum Concurrency Using C#

The following is a module with functions which demonstrates how to create multiple tasks with maximum concurrency using C#.

The examples demonstrated on this page uses System.Threading.Tasks.Task to start and run tasks. They also use System.Threading.SemaphoreSlim to limit the number of tasks that can run concurrently.

The examples on this page demonstrates how to start and run multiple tasks with a maximum concurrency. It also demonstrates how to start and run multiple tasks with a return value.


1. Task – Maximum Concurrency

The example below demonstrates how to start and run multiple tasks with a maximum concurrency. For example purposes, the tasks do not return a value.

The functions shown in the example below are called asynchronously, but they can also be called synchronously.


2. Task – Maximum Concurrency – Return Value

The example below demonstrates how to start and run multiple tasks with a maximum concurrency. In this example, a value is returned and retrieved from the tasks

The functions shown in the example below are called asynchronously, but they can also be called synchronously.


3. More Examples

Below is a full example of the process demonstrated on this page!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

C# || How To Convert Bytes To Kilobytes, Megabytes, Gigabytes, Terabytes Using C#

The following is a module with functions which demonstrates how to convert bytes to decimal formats like kilobytes, megabytes, gigabytes, terabytes, petabytes, exabytes, zettabytes, and yottabytes, as well as binary formats like kibibytes, mebibytes, gibibytes, tebibytes, pebibytes, exbibytes, zebibytes, and yobibytes using C#.

The function demonstrated on this page follows the IEC standard, which means:


• 1 kilobyte = 1000 bytes (Decimal)
• 1 kibibyte = 1024 bytes (Binary)

This function allows you to convert bytes to a measurement unit, a measurement unit to bytes, and allows to convert from one measurement unit to another measurement unit.


1. Convert Bytes To Measurement Unit

The example below demonstrates the use of ‘Utils.Bytes.FromTo‘ to convert bytes to a measurement unit.

The optional function parameter allows you to specify the decimal places.


2. Convert Measurement Unit To Bytes

The example below demonstrates the use of ‘Utils.Bytes.FromTo‘ to convert a measurement unit to bytes.

The optional function parameter allows you to specify the decimal places.


3. Convert Measurement Unit To Measurement Unit

The example below demonstrates the use of ‘Utils.Bytes.FromTo‘ to convert a measurement unit to another measurement unit.

The optional function parameter allows you to specify the decimal places.


4. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


5. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

C# || How To Parse A Delimited CSV File Using C#

The following is a module with functions which demonstrates how to parse a delimited CSV file using VB.NET.

This function parses a CSV file and returns its results as a List. Each List index represents a line in the CSV file, with each item in the list representing a record contained on that line.

The function demonstrated on this page uses FileIO.TextFieldParser to parse values in a CSV file.

Note: To use the function in this module, make sure you have a reference to ‘Microsoft.VisualBasic‘ in your project.

One way to do this is, in your Solution Explorer (where all the files are shown with your project), right click the ‘References‘ folder, click ‘Add Reference‘, then type ‘Microsoft.VisualBasic‘ in the search box, and add the reference titled Microsoft.VisualBasic in the results Tab.


1. Parse CSV File

The example below demonstrates the use of ‘Utils.Methods.ParseCsv‘ to parse a CSV file and return its results as a List.

The optional function parameter allows you to specify the delimiters. Default delimiter is a comma (,).

Sample CSV used in this example is the following:


2. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


3. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

C# || How To Round A Number To The Nearest X Using C#

The following is a module with functions which demonstrates how to round a number to the nearest X using C#.

This function has the ability to either round a number to the nearest amount, always round up, or always round down. For example, when dealing with money, this is good for rounding a dollar amount to the nearest 5 cents.


1. Round – Nearest

The example below demonstrates the use of ‘Utils.Round.Amount‘ to round a number to the nearest 5 cents.

The optional function parameter determines they type of rounding to perform.


2. Round – Up

The example below demonstrates the use of ‘Utils.Round.Amount‘ to always round a number up to the nearest 5 cents.

The optional function parameter determines they type of rounding to perform.


3. Round – Down

The example below demonstrates the use of ‘Utils.Round.Amount‘ to always round a number down to the nearest 5 cents.

The optional function parameter determines they type of rounding to perform.


4. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


5. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

C# || How To Replace A Letter With Its Alphabet Position Using C#

The following is a module with functions which demonstrates how to replace a letter with its alphabet position using C#.


1. Replace With Alphabet Position

The example below demonstrates the use of ‘Utils.Methods.GetAlphabetPosition‘ to replace a letter with its alphabet position.


2. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


3. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

C# || How To Generate A Random String Of A Specified Length Using C#

The following is a module with functions which demonstrates how to generate a random code of a specified length using C#.

The function demonstrated on this page has the ability to generate random strings that contains only letters, only numerical digits, or alphanumeric strings.


1. Random Code – Alphabetical

The example below demonstrates the use of ‘Utils.Code.GetRandom‘ to generate a code of a specified length that contains only letters.

The optional function parameter determines the type of code that is generated.


2. Random Code – Numeric

The example below demonstrates the use of ‘Utils.Code.GetRandom‘ to generate a code of a specified length that contains only digits.

The optional function parameter determines the type of code that is generated.


3. Random Code – Alphanumeric

The example below demonstrates the use of ‘Utils.Code.GetRandom‘ to generate a code of a specified length that is alphanumeric.

The optional function parameter determines the type of code that is generated.


4. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


5. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

C# || How To Get A List Of Files At A Given Path Directory Using C#

The following is a module with functions which demonstrates how to get a list of files at a given directory path using C#.

The function demonstrated on this page returns a list of System.IO.FileInfo, which contains information about the files in the given directory.


1. Get Files In Directory

The example below demonstrates the use of ‘Utils.Methods.GetFilesInDirectory‘ to get a list of files at a given path directory.

The optional function parameter lets you specify the search option. This lets you specify whether to limit the search to just the current directory, or expand the search to the current directory and all subdirectories when searching for files.


2. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


3. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

C# || How To Get The Computer & User Client IP Address Using C#

The following is a module with functions which demonstrates how to get the computers and user client request IPv4 IP address using C#.

The function demonstrated on this page returns the IPv4 address of the calling user. When under a web environment, it returns the clients System.Web.HttpContext.Current.Request IP address, otherwise it returns the IP address of the local machine (i.e: server) if there is no request.

The following function uses System.Web to determine the IP address of the client.

Note: To use the function in this module, make sure you have a reference to ‘System.Web‘ in your project.

One way to do this is, in your Solution Explorer (where all the files are shown with your project), right click the ‘References‘ folder, click ‘Add Reference‘, then type ‘System.Web‘ in the search box, and add the reference titled System.Web in the results Tab.


1. Get IP Address

The example below demonstrates the use of ‘Utils.Methods.GetIPv4Address‘ to get the IPv4 address of the calling user.


2. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


3. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

C# || How To Copy DataTable DataRow From One DataRow To Another Using C#

The following is a module with functions which demonstrates how to copy a DataTable DataRow from one DataRow to another using C#.

The function demonstrated on this page is an extension method, which copies all matching columns from the source DataRow to the destination DataRow. If no matching column exists between the two rows, the data at that column is skipped. This allows to safely copy a single row from one DataTable to other.


1. Copy DataRow

The example below demonstrates the use of ‘Utils.Collections.Extensions.CopyTo‘ to copy all matching columns from a source row to a destination row.


2. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


3. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

C# || How To Generate, Create & Read A QR Code Using C#

The following is a module with functions which demonstrates how to generate, create and read a QR code using C#.

The functions demonstrated on this page has the ability to create and encode a string to a QR code byte array, and another function to read and decode a byte array QR code to a string.

The following functions use ZXing.Net to create and read QR codes.

Note: To use the functions in this module, make sure you have the ‘ZXing.Net‘ package installed in your project.

One way to do this is, in your Solution Explorer (where all the files are shown with your project), right click the ‘References‘ folder, click ‘Manage NuGet Packages….‘, then type ‘ZXing.Net‘ in the search box, and install the package titled ZXing.Net in the results Tab.


1. Create QR Code – Encode

The example below demonstrates the use of ‘Utils.QRCode.Create‘ to create a QR code byte array from string data.

The optional function parameters allows you to specify the QR code height, width and margin.


2. Read QR Code – Decode

The example below demonstrates the use of ‘Utils.QRCode.Read‘ to read a QR code byte array and decode its contents to a string.


3. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


4. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

C# || How To Append & Join A Date & Time Value Together Using C#

The following is a module with functions which demonstrates how to append and join a date and time value together using C#.


1. Append Date & Time

The example below demonstrates the use of ‘Utils.Extensions.SetTime‘ to append a date and time value together.


2. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


3. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

C# || How To Generate Hourly Time Range With Minute Interval Using C#

The following is a module with functions which demonstrates how to generate an hourly time range with minute interval between a start and end time using C#.

The function demonstrated in this page generates a time value list containing the time from a starting hour to an ending hour, separated by a minute step. The starting and ending hour is an integer from 0 through 23 representing the 24 hour period of the day, and the minute step can be any minute interval, including decimal values.

The starting and ending hours can also be flipped, indicating that the results should be returned in descending order.

An optional parameter also exists which allows to tune the results. When the range is in ascending order, the parameter indicates that the results should exclude/include the entire ending hour. When the range is in descending order, the parameter indicates that the results should exclude/include the entire starting hour.


1. Ascending Time Range – Minute Interval – Default

The example below demonstrates the use of ‘Utils.DateRange.GetTimeRange‘ to get the time range with a minute interval in ascending order.

In this example, the default options are used, which excludes the full ending hour from the results.


2. Descending Time Range – Hourly Interval – Default

The example below demonstrates the use of ‘Utils.DateRange.GetTimeRange‘ to get the time range with a hourly interval in descending order.

In this example, the default options are used, which excludes the full starting hour from the results.


3. Ascending Time Range – Minute Interval – Include Full Hour

The example below demonstrates the use of ‘Utils.DateRange.GetTimeRange‘ to get the time range with a minute interval in ascending order.

In this example, the optional parameter is used, which includes the full ending hour from the results.


4. Descending Time Range – Hourly Interval – Include Full Hour

The example below demonstrates the use of ‘Utils.DateRange.GetTimeRange‘ to get the time range with a hourly interval in descending order.

In this example, the optional parameter is used, which includes the full starting hour from the results.


5. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


6. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

C# || How To Add & Use Custom Attributes To Class Properties Using C#

The following is a module with functions which demonstrates how to add and use custom attributes for class properties using C#.

The function demonstrated on this page is a generic extension method which uses reflection to get object property data transformed according to its custom attribute.


1. Custom Attribute

The example below demonstrates the use of ‘Utils.Extensions.GetPropertyData‘ to get object property data transformed according to its custom attribute.


2. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


3. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.