Search

Tutorials, tips&tricks, snippets..

#KISS: KEEP IT SIMPLE, STUPID!

Category

Miscellaneous

Set Current Directory For Windows Service

By default, the current directory for your Windows service is the System32 folder. I keep forgetting that which causes me problems when I try to access a file or folder using a relative path.

System.IO.Directory.SetCurrentDirectory(System.AppDomain.CurrentDomain.BaseDirectory);

Use the above line of code to set the current directory to the same directory as your windows service.

Create and install a Windows Service from a .NET Core project

Creating a Windows Service was the domain of the .Net Framework. However, since the release of the Windows Compatibility Pack that has all changed. There are no “New Windows Service (.Net Core)” options in Visual Studio, so how can we reach the goal?

Continue reading “Create and install a Windows Service from a .NET Core project”

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!!)”

Powered by WordPress.com.

Up ↑