Tag Archives: C#

Convert DateTime to Unix time in C#

I’ve been asked recently by a few people how to convert Unix time to DateTime format (and the other way around) so I decided to make a post about it explaining how Unix time works and provide a snippet to help people that want to convert DateTime to Unix time or vise versa.

Unix time is basically the number of seconds that have passed since 1/1/1970 00:00:00 (UTC). In order to convert a specific date and time to a Unix time value we will need to subtract the date above from the date we want to convert to Unix time.

Example:


Now if we want to convert a Unix time value to a DateTime object the the principle is the same. We take the 1/1/1970 00:00:00 (UTC) date and add the value of the Unix time as seconds to that date. The result would be the actual DateTime of the Unix time stamp.

Example:

Tracert implementation in C#

While I was looking for a tracert method that I could use for an application of mine I noticed that the .NET framework doesn’t provide one out of the box, so I decided to create one that matched my requirements.

The method below works in the exact same fashion the tracert method works. Providing
the method with the IP address, the max number of hops you would like to receive and a timeout value for the hops will allow you to track down the route your data will travel in order to get to their destination, as well as the time (in milliseconds) it will take for each hop.

Main method:

How to generate QR barcodes in C#

QR barcodes are machine-readable optical labels that contain certain information. Today we will be looking into how to generate QR codes with the use of the ZXing.Net library.

First you will need to download the ZXing.Net library from zxingnet.codeplex.com. Extract the contents of the file you have downloaded and reference the library that fits your needs in your project.

Using the example code below you will now be able to create your own QR codes.

Example: Calling the following method will return a Bitmap object which you can save using the Bitmap’s Bitmap.Save() method or simply display it within your application.

Fluxbytes QR code example

Unzip files using Shell32 in C#

The .NET Framework didn’t have an easy way for developers to unzip files resulting to a lot of people having to use third party libraries to achieve that functionality. This was until the .NET Framework 4.5 was released which had a new class called ZipFile that simplified the process of unzipping files..

But since targeting the .NET Framework 4.5 might not be ideal in many cases, mainly because its adaptation rate is still fairly low I will demostrate another way to unzip .zip files with the use of Shell32.

Firstly you will need to reference in your project the COM library called Microsoft Shell Controls And Automation (Interop.Shell32.dll) in order to be able to access the Shell32 namespace.

The method below is a simple example that takes two parameters. The .zip file location and the folder destination where the files will be extracted to.

The usage is fairly simple:

Creating and connecting to a Microsoft Access Database programmatically in C#

The following example aims to get you a bit more familiar as to how to create and connect to a Microsoft Access database programmatically while being able to add any type of tables you want as well are inserting and retrieving data from it.

Firstly we will need to reference two libraries in our project. Microsoft ActiveX Data Objects 2.0 Library (Interop.ADODB.dll) which we will use to ensure that the connection to the database is closed after we are done creating it, and Microsoft ADO Ext. 2.8 for DDL and Security (Interop.ADOX.dll) which is needed to be able to access the classes that are required to create our database.

After you have finished adding those two references to your project you can use the code sample below to create the database file programmatically. Feel free to modify the code to suit your needs. Also please note that setting your project to Any CPU build will cause exceptions to be thrown when creating the database.