.Net assembly loading from UNC path fails without modify rights on 64bit

时间:2015-10-06 08:25:08

标签: c# python .net matlab python.net

I have a problem with .Net assemblies not being found when I try to load them from a 64 bit process if the user loading the assemblies does not have sufficient access rights.

Loading the same assemblies from a 32 bit process is not a problem and if the user running the 64 bit process is given modify rights the loading is not a problem.

The dll files are located on a file share (I assume it is NTFS, but not entirely sure) and loading from a UNC path.

The dlls are not loaded in a normal .Net program but rather used in Python through Python.Net and in Matlab through the normal Matlab .Net integration. The problem is the same in Python and Matlab, so that suggest that the problem is on the .Net side.

1 个答案:

答案 0 :(得分:4)

There is a version of AppDomain.CurrentDomain.Load() that accepts a byte[] containing the file content of the DLL to load. Using this you can manually load pretty well any .NET Assembly that you can access, whether it's on the local drive, network, compressed, resource, downloaded, etc.

In C# you can use something like this:

public static Assembly LoadAssembly(string filename)
{
    var content = System.IO.File.ReadAllBytes(filename);
    return AppDomain.CurrentDomain.Load(content);
}

Not even going to try to translate that into Python.NET or Matlab.

Since the file read is not involved with the assembly load directly it won't trigger the security concerns of loading a remote assembly. Also has the added advantage that the DLL file is never locked longer than it takes to read it fully into memory. Does take slightly more memory to do however.