我做了一个自定义控件(EB.cs),该cs文件编译成dll并为其他工程所调用。
现在,我希望能知道dll文件所在的位置,该在cs文件中用什么语句?
例如,
我的cs文件在c:\EB.cs,编译dll后放在c:\dll\EB.dll中;
新项目EB中的文件c:\project\EB.aspx引用了EB.dll文件;EB.aspx为启动页面;
EB.dll文件绑定了EB.aspx上的一个Label控件(EB.dll中的一个自定义属性,手动设置)
我希望项目运行时,Label控件能显示出EB.dll文件所在的目录“c:\dll”来;
应该在cs文件中添加怎样的语句。

解决方案 »

  1.   

    在 eb 文件中使用:this.GetType().Assembly.CodeBase 可以返回程序集所在的位置
      

  2.   

    可以这样来写:public string GetDllPath()
    {
        return System.Reflection.MethodInfo.GetCurrentMethod().ReflectedType.Module.FullyQualifiedName;}
      

  3.   

    在.cs文件中定义一个方法,返回路径,通过
    System.Reflection.Assembly的CodeBase属性可返回程序集的路径下面的示例显示一个使用 CodeBase 属性的表达式。
    [Visual Basic] 
    Dim SampleAssembly As [Assembly]
    ' Instantiate a target object.
    Dim Integer1 As New Int32()
    Dim Type1 As Type
    ' Set the Type instance to the target class type.
    Type1 = Integer1.GetType()
    ' Instantiate an Assembly class to the assembly housing the Integer type.  
    SampleAssembly = [Assembly].GetAssembly(Integer1.GetType())
    ' Gets the location of the assembly using file: protocol.
    Console.WriteLine(("CodeBase=" + SampleAssembly.CodeBase))
            End Sub 'Snippet1
    [C#] 
    Assembly SampleAssembly;
    // Instantiate a target object.
    Int32 Integer1 = new Int32();
    Type Type1;
    // Set the Type instance to the target class type.
    Type1 = Integer1.GetType();
    // Instantiate an Assembly class to the assembly housing the Integer type.  
    SampleAssembly = Assembly.GetAssembly(Integer1.GetType());
    // Gets the location of the assembly using file: protocol.
    Console.WriteLine("CodeBase=" + SampleAssembly.CodeBase);
      

  4.   

    也可以写成一个静态的方法,这样用的更方便一些(这个方法可是从思归老大那里学来的):public static string GetDllPath()
    {
    return System.Reflection.MethodInfo.GetCurrentMethod().ReflectedType.Module.FullyQualifiedName;
    }
      

  5.   

    一个更简单的:public static string GetDllPath()
    {
    return System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
    }或:public string GetDllPath()
    {
    return System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
    }