2

I use ANTLR for parsing a document, and I need some ANTLR dlls. For compilation, I can use /lib: to located where the ANTLR dll libraries are located, but when I run the execution binary I got error message.

Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'Antlr3.Runtime, Version=3.1.3.421
54, Culture=neutral, PublicKeyToken=3a9cab8f8d22bfb7' or one of its dependencies. The system cannot find the file specif
ied.
   at Antlr.Examples.LLStar.LLStarApp.Main(String[] args)

The error is gone when I copy the ANTLR dll in the same directory where the execution binary is located.

I added the directory where the dlls are located in the path, it still doesn't work. How can I setup an environment variable or something to find the dlls? I use Windows 7.

ADDED

I guess there is no way to use PATH or environment variable to do this, I think GAC is one solution, and Set Custom Path to Referenced DLL's? is the other solution, even though it can find only the sub-directories beneath the directory where the execution binary is located.

Community
  • 1
  • 1
prosseek
  • 182,215
  • 215
  • 566
  • 871
  • This is a DLL Hell countermeasure. Copying the DLL locally is the correct way to do it. Using AppDomain.AssemblyResolve is questionable. – Hans Passant Oct 13 '11 at 22:01

3 Answers3

3

AppDomain.CurrentDomain.SetupInformation.PrivateBinPathProbe

or

AppDomain.CurrentDomain.AssemblyResolve += (sndr,resolveEventArgs) =>
{
    if(resolveEventArgs.Name==something){
        return Assembly.LoadFile(assemblyPath);
    }
    return null;
};
L.B
  • 114,136
  • 19
  • 178
  • 224
0
    static void AddEnvironmentPaths(IEnumerable<string> paths)
    {
        var path = new[] { Environment.GetEnvironmentVariable("PATH") ?? string.Empty };

        string newPath = string.Join(Path.PathSeparator.ToString(), path.Concat(paths).ToArray());

        Environment.SetEnvironmentVariable("PATH", newPath);
    }
DaNeSh
  • 1,022
  • 1
  • 14
  • 24
0

You can use the Environment class for this.

Specifically the GetEnvironmentVariable and SetEnvironmentVariable methods (in this case with the PATH environment variable.

Use GetEnvironmentVariable to retrieve the current settings, then add your path to the returned string and use SetEnvironmentVariable to update it.


Of course, you could do all this manually.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • Do I really need to change C# source code to find the dll at runtime? For mono, I just set environment variable to find it. – prosseek Oct 13 '11 at 20:05
  • @prosseek - No, you don't. Hence my last line about doing it manually... Though you didn't specify what OS you are using - this is slightly different in the different versions of Windows. – Oded Oct 13 '11 at 20:05