【问题标题】:Change the working directory for a specific static bound assembly更改特定静态绑定程序集的工作目录
【发布时间】:2015-02-18 00:04:28
【问题描述】:

我在项目设置中引用了一个外部程序集 DLL(说它安装在 c:\ABC)。当我从该程序集中调用导出的方法时,出现运行时错误。因为它正在寻找文件 c:\ABC\abc.ini 而 .exe 工作目录位于其他位置。

如何仅“欺骗”当前工作目录位于 c:\ABC 的程序集?

【问题讨论】:

    标签: c# .net dll .net-assembly


    【解决方案1】:

    假设您无法控制该外部程序集(即您无法修改其代码),您唯一能做的就是将对该程序集的所有调用包装为:

    var currentDir = Environment.CurrentDirectory;
    Environment.CurrentDirectory = @"c:\ABC";
    try
    {
        // call that external assembly
    }
    finally
    {
        Environment.CurrentDirectory = currentDir;
    }
    

    【讨论】:

    • 谢谢,但它不起作用。 Directory.SetCurrentDirectory 也没有
    • 哦,我认为程序集不使用当前目录来查找 abc.ini。它使用 Assembly.GetExecutingAssembly().CodeBase 或 Assembly.GetExecutingAssembly().Location。那有可能骗它吗?
    • 我发布了另一个问题 stackoverflow.com/questions/27561242/… 以获取装配位置。这个可以关了。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-08
    • 1970-01-01
    • 2016-06-12
    • 1970-01-01
    • 2012-06-27
    • 1970-01-01
    相关资源
    最近更新 更多