如何在运行时获取程序集路径,在webserver,service或win app中运行

时间:2013-02-26 19:31:08

标签: c# reflection resources

我有一个资源项目,我用于产品的各个部分。为了使其更加灵活,我决定将.resx放在使用resgen工具处理的dll外部,以允许用户动态添加自己的语言文件。由于项目将从各个地方,网站,服务或独立的winForm访问,我如何获得.dll所在的路径,以便我可以提供正确的路径

[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
    public static global::System.Resources.ResourceManager ResourceManager {
        get {
            if (object.ReferenceEquals(resourceMan, null)) {
                temp = ResourceManager.CreateFileBasedResourceManager("Resources", cwd, null); 
                resourceMan = temp;
            }
            return resourceMan;
        }
    }

我可以对路径进行硬编码并且效果很好,但我宁愿让它在运行时找出路径。

我尝试了类似的东西,但是它不起作用

string fullPath = System.Reflection.Assembly.GetAssembly(typeof(Resources)).Location;

//get the folder that's in
string theDirectory = Path.GetDirectoryName(fullPath); ResourceManager temp =

"Could not find any resources appropriate for the specified culture (or the neutral culture) on disk.

baseName:Resources locationInfo:fileName:Resources.resources“

1 个答案:

答案 0 :(得分:0)

找到以下工作

How do I get the path of the assembly the code is in?

string codeBase = Assembly.GetExecutingAssembly().CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
var path = Uri.UnescapeDataString(uri.Path);
var theDirectory = Path.GetDirectoryName(path);