類庫編譯為 DLL,運行時需讀取同目錄下的配置文件。
' 1. 獲取啟動程序集路徑(EXE 所在目錄)
Dim exePath = System.Windows.Forms.Application.StartupPath
' 2. 獲取當前程序集(DLL)路徑
Dim asm = System.Reflection.Assembly.GetExecutingAssembly()
Dim dllDir = System.IO.Path.GetDirectoryName(asm.Location)
' 或
Dim dllDir = System.IO.Path.GetDirectoryName(asm.CodeBase.Replace("file:///", "").Replace("/", "\"))
Application.StartupPath。Assembly.Location。在類庫中暴露初始化方法,由宿主程序傳入配置路徑,避免耦合文件系統結構。