Tutorials, tips&tricks, snippets..




C# core: how to generate a pdf from a html with a xsl stylesheet as base64 stream

My goal was to create a base64 string representing the pdf, then send it to my mobile app and show it to the user (here for more info).

The following code is written using the Select.HtmlToPdf.NetCore package.

Continue reading “C# core: how to generate a pdf from a html with a xsl stylesheet as base64 stream”


Round number keeping trailing decimals without using string

I need all values to rounded to 8 decimal places. So, for example, 1.2 should round to 1.20000000, but Math.Round(value, 8) rounds to 1.2.

I’m using a C# class to populate and then serialise to xml. One of the values is a double. If I assign a ‘1.2’ to the value this gets serialised to ‘1.2’ when I actually need ‘1.20000000’.

I found this fast way to do this without converting the number to string:

foo = doubleValue + 0.00M;

And that makes the value 1.20000000 instead of just 1.2. This was better then doing a ToString and then parsing it back.

Convert a List<int> to a List<int?>

Here a very fast but useful snippet:

List<int?> nullList = myIntList.ConvertAll<int?>(x => x);

VIES: using European VAT Verification SOAP Service

EU created a service to obtain confirmation of the validity of the VAT identification number of any specified person. This service works with SOAP and can validate all the EU VAT. You just need to send the state code and the VAT.

This service works within the VIES environment (VAT Information Exchange System)

Continue reading “VIES: using European VAT Verification SOAP Service”

Traceroute an IP and list the hops using C#

This code snippet perfoms traceroute (“tracert command”) an IP and allow you to know all the hops with some infos, like hostname, reply time address and other stuff.

Below the code.
Continue reading “Traceroute an IP and list the hops using C#”

C#: right pad an array

Here there is a code snippet showing how to right pad an array (in this case, a byte array).

public static byte[] ByteArrayLeftPad(byte[] input, byte padValue, int len)
    var temp = Enumerable.Repeat(padValue, len).ToArray();
    for (var i = 0; i < input.Length; i++)
        temp[i] = input[i];

Here you can find how to left pad an array.

Wait Until A Condition Is True (and not stuck the CPU!!)

Sometimes the need arises to wait for a condition or property to become true, this is especially useful for unit / integration tests. The System.Threading namespace contains SpinWait which will enable use to enter a condition that we want to wait on to see if it becomes true.

Continue reading “Wait Until A Condition Is True (and not stuck the CPU!!)”

HOWTO: close the TCP connection of other process calling Windows API

We know that TCPview, for example, can close the TCP connection for the other process without closing the process. So how to implement it? Suppose there is a process connecting to the a remote ip and port, how to terminate that connection?

Continue reading “HOWTO: close the TCP connection of other process calling Windows API”

Fast and useful hint to analyze your disk usage

Just click here and discover the power of WinDirStat. If you have a full disk to clean up, this will be your savior.

Powered by

Up ↑