1. 程式人生 > >獲得引用程式集的路徑

獲得引用程式集的路徑

建立一個Windows Application 接受其預設名稱WindowsApplication1

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

應用程式域:

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

環境變數:

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

用反射:

但若自定義了一個類庫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的源程式集