smokeing
smokeing

Reputation: 259

Mono - invalid path returned from Path.GetDirectoryName

var codebase = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
var path = Path.GetDirectoryName(codebase);

It does work on windows. On mono version 4.2.1 the result is:

codebase: file:///home/...
path: file:/home/...

Is this a bug, or am I doing something wrong?

Upvotes: 0

Views: 602

Answers (1)

smokeing
smokeing

Reputation: 259

I have found solution.

Uri should be constructed from CodeBase directly because it returns path in Uri format. Then Uri.LocalPath returns local path to assembly (in path format) and here is place where GetDirectoryName can be used.

I have made an extension:

using System;
using System.IO;
using System.Reflection;
public static class AssemblyExtensions
{
    public static string GetCodeBaseLocation(this Assembly assembly)
    {
        var uri = new Uri(assembly.CodeBase);
        return Path.GetDirectoryName(uri.LocalPath);
    }
}

Upvotes: 1

Related Questions