Dot-Net
查找目前配置文件的名稱
有沒有辦法從類庫中找到目前應用程序配置文件的名稱(和路徑)?
例如,在 Web 應用程序中是這樣
web.config,在 Windows 應用程序或服務中是這樣myapp.exe.config。
嘗試這個:
AppDomain.CurrentDomain.SetupInformation.ConfigurationFile如果這不起作用,請添加對 System.Web 和 System.Configuration 的引用,然後嘗試以下操作:
if(HttpRuntime.AppDomainAppVirtualPath == null) return ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath; else return VirtualPathUtility.ToAbsolute("~/Web.config");
獲取此 Configuration 對象表示的配置文件的物理路徑。