获得引用程序集的路径

创建一个Windows Application 接受其默认名称WindowsApplication1

若想获得WindowsApplication1的所在目录,可以用以下方法:

应用程序域:

AppDomain.CurrentDomain.BaseDirectory  => E:/WindowsApplication1/WindowsApplication1/bin/Debug/ 

环境变量:

Environment.CurrentDirectory   =>  E:/WindowsApplication1/WindowsApplication1/bin/Debug

用反射:

Reflection.Assembly.GetAssembly(GetType(WindowsApplication1.Form1)).CodeBase  =>  file:///E:/WindowsApplication1/WindowsApplication1/bin/Debug/WindowsApplication1.EXE

但若自定义了一个类库Class1,公开了3个属性MyPath1,MyPath2,MyPath3,生成dll后被WindowsApplication1引用

Public Class Class1

  shared  ReadOnly Property MyPath1() As String
        Get
            Return AppDomain.CurrentDomain.BaseDirectory 
        End Get
    End Property

   shared   ReadOnly Property MyPath2() As String
        Get
            Return Environment.CurrentDirectory
        End Get
    End Property

  shared    ReadOnly Property MyPath3() As String
        Get
            Return Reflection.Assembly.GetAssembly(GetType(Class1)).CodeBase
        End Get
    End Property

End Class

在WindowsApplication1中查询Class1的MyPath1,MyPath2和MyPath3.发现,MyPath1、MyPath2返回的并不是被 引用的Class1的特征,而是而只有MyPath3返回的才是Class1的文件位置。可见反射是多么强大的功能。

同样,反射也可以确定系统组件的路径:

获得TextBox的源程序集

Reflection.Assembly.GetAssembly(GetType(TextBox)).CodeBase  =>  file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值