Friendly Name

Friendly Name

There is a property on the AppDomain type, called FriendlyName that I use often. One thing about it though, I often have to follow up the call to FriendlyName with some code to strip off the trailing file extension. I hate to repeat myself, so, I wrapped that code into an extension method I call FriendlyNameEx, here is the code for that:

public static string FriendlyNameEx(
    this AppDomain appDomain,
    bool stripTrailingExtension = false
    )
{
    Guard.Instance().ThrowIfNull(appDomain, nameof(appDomain));

    var friendlyName = AppDomain.CurrentDomain.FriendlyName;
    if (friendlyName.Contains("Enumerating source"))
    {
        var match = Regex.Match(friendlyName, @": Enumerating source \((?<path>.*)\)");
        if (match.Groups.Count >= 2)
        {
            friendlyName = Path.GetFileName(match.Groups[1].Value);
        }
        else
        {
            friendlyName = Path.GetFileName(
                Process.GetCurrentProcess().MainModule.FileName
                );
        }
    }

    if (stripTrailingExtension)
    {
        if (friendlyName.ToLower().EndsWith(".dll") ||
            friendlyName.ToLower().EndsWith(".exe"))
        {
            friendlyName = Path.GetFileNameWithoutExtension(friendlyName);
        }
    }
    return friendlyName;
}

The method starts by validating the incoming AppDomain parameter. Then is uses the existing FriendlyName property, on the AppDomain, to set the friendlyName variable. I’ll use that string value as the base for everything else I do in the method.

Next is a comparison that may, at first glance, seem a bit odd. Let me explain. When I run this method on my build server, I notice that sometimes the name returned by the FriendlyName property, is actually the name of the test container, not the name of my library, or exe, or whatever. That’s a problem for unit testing because I don’t expect to see the name of a test container there. So, I added the comparison here in an attempt to capture this scenario and deal with it. If the name returned from the FriendlyName property begins with the value “Enumerating source”, I’ll assume the code is actually running in a text fixture, and so, can’t return the name I’m expecting. In that event, I then move on to try to extract the actual friendly name, from that string. I do that with a regular expression. If that REGEX captures correctly, I use it to set the friendlyName variable. If not, I try to manufacture a reasonable friendly name using the name of the current process’s main module.

Either way, after that block of code has finished, I’ll have some soft of reasonably friendly name to work with. The only thing left to do, at that point, is to decide if I need to strip off the trailing file extension, or not. I’ll do that by first checking the value of the stripTrailingExtension parameter. If it contains true, then I used the Path.GetFileNameWithoutExtension method to perform that bit of string surgery, for me.

Only thing left to do, after all that, is return the friendly name to the caller.

Caling this method couldn’t be easier, just do something like this:

void Test()
{
   var friendlyName = AppDomain.CurrentDomain.FriendlyNameEx();
  //  Use the friendlyName value here ...
}

The code for this method, and others like it, can be found in the CODEGATOR CG.Core NUGET package, HERE. The source code is published in GITHUB HERE.

Photo by Nick Page on Unsplash