[Mono-dev] Class built by mono throws FileNotFoundException when run on windows

Edward Ned Harvey (mono) edward.harvey.mono at clevertrove.com
Tue Sep 1 20:04:57 UTC 2015


I've always used separate project files on windows and linux, in order to include different compiler symbols, in order to make projects build with different dependencies. I've been chastised here for doing it, so I'd like to find a better way. (Miguel and others tore apart a pull request, saying I should never check __MONO__, if I need behavior to be different on windows and non-windows, I need to use a runtime check. The problem is, as described below, the runtime check can't build or run on windows, so I'd like to figure out how it should be done).

Right now, I have a class, which is using Mono.Unix.Native, because of a method that does this: 
    if (Type.GetType("Mono.Runtime") != null) {
        Syscall.chmod(...);
    }

When built and run on mono, works great. The problem is building and running on windows. In order to make it build, I copied Mono.Posix.dll into the project and referenced it, with CopyLocal = False. This way, Mono.Posix.dll doesn't get copied to the build directory, which is good because it's already present on mono systems, and not needed on windows systems - the only reason for it to exist in the project is because windows can't build without it.

So it builds. But unfortunately, it won't run on windows. It throws FileNotFoundException "Mono.Posix.dll" before evaluating the if-clause.

The workaround I've found is to create a wrapper class MonoSpecific, so the if-clause and the Mono.Posix call are not in the same file. But this is clearly a hack. Is there a better way?

Hello.cs:
    using System;
    namespace helloProject
    {
        static class Hello
        {
            static void ChangePermsIfNecessary()
            {
                if (Type.GetType("Mono.Runtime") != null) {
                    MonoSpecific.DoChmod();
                }
            }
        }
    }

MonoSpecific.cs:
    using System;
    using Mono.Unix.Native;
    namespace helloProject
    {
        static class MonoSpecific
        {
            static void DoChmod()
            {
                Syscall.chmod(...);
            }
        }
    }



More information about the Mono-devel-list mailing list