What would you expect the following piece of Code to print. if the directory 'A' doesn't exist

@ECHO OFF
IF '1'=='1' (
        CD a
        ECHO %ERRORLEVEL%
)

CD a
ECHO %ERRORLEVEL%

Not very intuitive right?

This is because the DOS batch processor treats the whole if statement as one command, expanding the variables only once, before it executes the conditional block. So you end up with %ERRORLEVEL% being expanded to its value, which is 0, before you start the block, You can get around this by enabling Delayed Expansion. As the name suggests this forces the the Batch Processor to only expand variables once required to do so in the middle of execution.
To enable this behavior you need to do 2 things.
  1. SET ENABLEDELAYEDEXPANSION at the top of your script.
  2. Replace % delimited variables with an Exclamation. i.e. %ERRORLEVEL% becomes !ERRORLEVEL!

Now our script looks like this, and behaves as expected.

Working Script

@ECHO OFF
REM Enable Delayed Expansion
setlocal enabledelayedexpansion
IF '1'=='1' (
        CD a
        REM Use Exclamations instead of percentages
        ECHO !ERRORLEVEL!
)

CD a
ECHO %ERRORLEVEL%
For when powershell just isn't retro enough ;-)
~Eoin Campbell

Windows Services Windows Services

If you've ever worked with windows services, you'll know that they're a very powerful tool to have in your background processing arsenal. Unfortunately they can be also quite a pain to work with in developer land. Recently we've been spinning up a lot of new windows service projects in work as part of a Business Intelligence Data Processing Project. I thought this would be a good time to brain dump some of the tips & tricks I've come across over the past few years for dealing with .Net Windows Services.

I'll look at the basics for getting a service up and going, using the built project installer & Install Util. Then I'll take a look at easier ways of running the service inside the IDE, and how to run the service in user interactive mode.

Finally I'll look at ways to make the service self-installing without having to rely upon the InstallUtil.exe as well as gaining access to configuration settings during the installation process.

[important]The completed solution can be found on GitHub at https://github.com/eoincampbell/demo-windows-service [/important]

Here's a handy little code snippet to figure out if a local windows user account is enabled or not using the System.DirectoryServices namespace.

private static bool IsUserAccountEnabled(string username)
{
    try
    {
        var result = new DirectoryEntry { Path = "WinNT://" + Environment.MachineName + ",computer" }
            .Children
            .Cast<DirectoryEntry>()
            .Where(d => d.SchemaClassName == "User")
            .First(d => d.Properties["Name"].Value.ToString() == username);

        return ((int)result.Properties["UserFlags"].Value & 2) != 2;
    }
    catch
    {
        return false;
    }
}

~Eoin Campbell

nuget.org nuget.org

[important]
This is part 2 of a 2 part post on Combinatorics in .Net

The solution is publicly available on github; https://github.com/eoincampbell/combinatorics

The library can be added to any .NET Soution via Nuget; https://nuget.org/packages/Combinatorics
[/important]

In the last post we looked at the Combinatorics Library, a .NET Assembly which provides Combinatoric generation capabilities to your .NET Applications. Now lets look at bundling up that solution & deploying the package to Nuget. Nuget is an online .NET Package Repository & associated Visual Studio extension that makes it easy to manage external assemblies in your projects. Developers who build 3rd party libraries or tools can create a NuGet package and store the package in a NuGet repository. Other developers can then browse the repository (online or with Visual Studio) and add references to those 3rd party tools & libraries.

You can read more about Nuget here.

How many combinations? How many combinations?

[important]
This is part 1 of a 2 part post on Combinatorics in .Net

The solution is publicly available on github; https://github.com/eoincampbell/combinatorics

The library can be added to any .NET Soution via Nuget; https://nuget.org/packages/Combinatorics
[/important]

 

Recently while working on a project, I had need to generate combintations and permutations of sets of Inputs. In my search for a decent combinatorics library for .NET, (something which is missing from the BCL), I came across a Codeproject Article from Adrian Akision. The implementation included a C# Generics Collection implementation for creating Combinations, Permutations & Variations from an Input Set. Adrian very graciously allowed that I bundle up the code as a Class Library Solution on Github & then release the library via Nuget.