开发者

Setup environment variable to find the dlls at runtime with C#

开发者 https://www.devze.com 2023-04-12 17:00 出处:网络
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 m

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 th开发者_如何学Goough it can find only the sub-directories beneath the directory where the execution binary is located.


AppDomain.CurrentDomain.SetupInformation.PrivateBinPathProbe

or

AppDomain.CurrentDomain.AssemblyResolve += (sndr,resolveEventArgs) =>
{
    if(resolveEventArgs.Name==something){
        return Assembly.LoadFile(assemblyPath);
    }
    return null;
};


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.


    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);
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号