M4N*_*M4N 8 .net configuration app-config web-config
有没有办法从类库中查找当前应用程序的配置文件的名称(和路径)?
例如,在Web应用程序中,这将是web.config一个Windows应用程序或服务myapp.exe.config.
SLa*_*aks 16
试试这个:
AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
Run Code Online (Sandbox Code Playgroud)
如果这不起作用,添加对System.Web和System.Configuration的引用,然后尝试:
if(HttpRuntime.AppDomainAppVirtualPath == null)
return ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath;
else
return VirtualPathUtility.ToAbsolute("~/Web.config");
Run Code Online (Sandbox Code Playgroud)