Monday, 30 January 2017

Creating self extracting zip EXE under Linux for windows

Recently, I had a need to create a self extracting zip archive, which will open on any windows machine. Because, I am using Linux, I did not want to go through the process of installing the windows with 7z on it just for a sake of creating that archive. I was expecting that the process will be not trivial, as some of internet resources show, but all turned out very simple. Peazip archiver available for Windows as well as Linux, which is able to create self extracting exe archive. I downloaded portable version for 64 bit machine and when creating a new archive you get an option to create self extracting file in a single volume or split by chosen size.



What I found, peazip creates small archives that extract under windows machine just fine. If you need to zip large amount of files say > 1 GB, I would suggest to create normal archive, which can span several files of set size and then use 7zip on windows computer to extract them.

References:


Monday, 5 December 2016

Reading appsettings.json in .Net Core Console Application under Linux

Recently I was looking at how to load and use appsettings.json file in .Net Core Console application. Many resources on .Net forums and sites were descripting use of appsettings.json file in ASP.NET Core web App, but very little described appsettings with .Net Core Console app. I managed to created sample .Net Core app with the minimum required libraries. Creation of this sample program I am going to describe here. Let's call our App "appsettings".
$mkdir appsettings
$cd appsettings
$dotnet new
$dotnet restore
Now create appsettings.json file:
{
    "AppSettings": {
        "Date": "1900-01-01"
    }
}
Date is the setting I am interested in loading from AppSettings section.

Edit project.json file to include "Microsoft.Extensions.Configuration.UserSecrets": "1.1.0" in dependencies section. This is the only extra library we need in order to use ConfigurationBuilder functions.
{
    "version": "1.0.0-*",
    "buildOptions": {
        "debugType": "portable",
        "emitEntryPoint": true,
        "outputName": "AppSettings"
    },
    "dependencies": {},
    "frameworks": {
        "netcoreapp1.0": {
            "dependencies": {
                "Microsoft.NETCore.App": {
                    "type": "platform",
                    "version": "1.1.0"
                },
                "Microsoft.Extensions.Configuration.UserSecrets": "1.1.0"
            },
            "imports": "dnxcore50"
        }
    }
}
Now lets edit the Program.cs file. We are going to load the Date setting and display it in Console. Don't forget to include "using Microsoft.Extensions.Configuration;"
using System;
using System.IO;
using Microsoft.Extensions.Configuration;

namespace ConsoleApplication
{
    public class Program
    {
        public static string AppSettingsFile = "appsettings.json";
        public static string AppSettingsSection = "AppSettings";
        public static string DateSetting = "Date";

        public static void Main(string[] args)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile(Program.AppSettingsFile, optional: true, 
                             reloadOnChange: true);
            
            var config = builder.Build();
            var appSettings = config.GetSection(Program.AppSettingsSection);
            string dateString = appSettings[Program.DateSetting];
            Console.WriteLine("Date:" + dateString);
        }
    }
}
That is pretty much all. Now let's build and run it.
$dotnet build
$dotnet run
Date:1900-01-01

Monday, 14 November 2016

xUnit Theory test ClassData with complex type

xUnit.net is a powerful, free, open source, unit testing tool for the .NET Framework. xUnit uses different notation to NUnit or MSUnit to specify test with parameters. Here I am going to show how to use Theory ClassData with complex type.

I have a sample class Plant, which has function 'bool IsEqual(Plant plant);'. We will create a Theory test for it.
[Theory] [ClassData(typeof(IsEqualTestData))] public void IsEqual(Plant plant1, Plant plant2, bool expectedResult) { Assert.Equal(expectedResult, plant1.IsEqual(plant2)); }
Class IsEqualTestData is a ClassData used with complex type Plant. It generates a list of arrays of 3 objects Plant1, Plant2, and ExpectedResult boolean.
private class IsEqualTestData : IEnumerable<object[]> { private readonly List<object[]> _data = new List<object[]> { new object[] { new Plant() { Name = PlantTester.PlantName1, Description = PlantTester.PlantDescription1 }, new Plant() { Name = PlantTester.PlantName2, Description = PlantTester.PlantDescription2 }, false }, new object[] { new Plant() { Name = PlantTester.PlantName1 }, new Plant() { Name = PlantTester.PlantName1 }, true }, new object[] { new Plant() { Name = PlantTester.PlantName1, Description = PlantTester.PlantDescription1 }, new Plant() { Name = PlantTester.PlantName2, Description = PlantTester.PlantDescription1 }, true } }; public IEnumerator<object[]> GetEnumerator() { return _data.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } }
When running in xUnit it will report result of running each entry in the test.

If you are using Visual Studio you can download snippet file from github repository.

Reference:

xUnit_ClassData.snippet
Using dotnet watch test for continuous testing with .NET Core and XUnit.net - Scott Hanselman

Tuesday, 5 July 2016

.Net Core 1.0 Released! Or installing .Net Core on Debian

This was a great news to me that .Net Core 1.0 was released. I already was trying .Net Core RC2 on my Debian 8.x so I prepared to go through the installation process of new release. Surprisingly installation of .Net Core 1.0 described in Install for Debian 8 on Microsoft site was very smooth in comparison to beta versions. All I had to do is to clean up my previous install from ~/.dotnet go through the instructions on install page and "Hello World!" programs runs. Very pleased to see that improvement.

So here I am going to provide some links to what to do next once you get your install done too.

References:





Sunday, 26 June 2016

Recording Music CDs under Win 7


From days of Windows 95, Windows 98, Windows XP CD/DVD burners and writers were only coming out on the market and computer users were required to install additional software, which will allow them to burn CD and DVD disks. Disk writing software like Nero was included for free with the CD/DVD burner you could buy for your desktop computer or included on new laptop. Modern Windows operating system like Windows 7, Vista, Windows 8 and Windows 10 already have built in support for burning CD / DVD music, data and video disks. Users can use included Windows Media Player to rip and burn music CDs.

Under Windows 7 to burn audio CDs insert CD disk in disk drive and in Windows Media Player open Burn tab, drag music files you want to burn. The blue bar shows how much space left on the disk. Once files are selected click Start burn to start recording on the disk.


Data CDs / DVDs can be burned directly from Windows Explorer. Disk image files with ISO extension can be burned using right mouse click on the file and selecting "Burn disk image".

If you still require more features than the basic functions to burn disks look through available free and commercial applications available on sites like filehippo

References: