Reputation: 259
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
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