1

I'm writing a C# application that's being compiled and bundled with Mono's mkbundle, and then run.

I'm attempting to access the folder that the executable is being run in, which works when I run the application from the directory that it's in, using ./example. However, when running it relatively from another directory, such as ../Builds/example, it returns the wrong directory, instead returning the place it was executed from.

This all works perfectly on Windows, however I also need it to work for Linux too.

The methods I've previously tried are:

  • AppDomain.Current.BaseDirectory
  • Path.GetDirectoryName(Assembly.ExecutingAssembly().Location) (also tried EntryAssembly)

All of these return the incorrect directory, rather than the actual location of the executable.

Larry Tang
  • 642
  • 5
  • 23
  • This is a problem with Mono - their implementation of that API is perhaps iffy. ode. You may need to devise a workaround for Mono. But first of all, look at how exactly does the mkbundle's output look. E.g. whether it's a script that runs a temporary binary, etc. This may give you hints as to what the workaround may be. You'd also do well to look into Mono's sources to see if there's clearly a problem there. It may be a problem with the interaction of mkbundle with Mono's .NET implementation. – Kuba hasn't forgotten Monica Aug 25 '19 at 17:53
  • 1
    Per the duplicate question's answer: *`System.Reflection.Assembly.GetExecutingAssembly().CodeBase` will return the 'permenant' path of the assembly.* Try that. – Kuba hasn't forgotten Monica Aug 25 '19 at 17:54
  • @KubaOber This also fails. The problem is Mono's implementation of mkbundle creating a temporary executable in the executing directory, so I'm not sure how this can be resolved. – Larry Tang Aug 25 '19 at 18:43

1 Answers1

1

I found a mostly reliable method to find the path to mkbundle executables, however I'm not confident that this is the optimal solution.

Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName)

Larry Tang
  • 642
  • 5
  • 23