有没有办法找出app.config文件是否存在,而不使用"File.Exists"?我试过了
if ( !ConfigurationManager.ConnectionStrings.ElementInformation.IsPresent )
{...}
Run Code Online (Sandbox Code Playgroud)
但即使带有连接字符串的app.config存在,IsPresent也是假的.
编辑:我是否误解了IsPresent属性?
Rob*_*ine 20
AppDomain关于应用程序的配置文件所在位置的报告.您可以测试文件是否确实存在(不需要虚拟AppSettings,也不需要尝试找出应该调用的配置文件或它的位置):
public static bool CheckConfigFileIsPresent()
{
return File.Exists(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
}
Run Code Online (Sandbox Code Playgroud)
您可以在静态类中创建方法
public static class ConfigurationManagerHelper
{
public static bool Exists()
{
return Exists(System.Reflection.Assembly.GetEntryAssembly());
}
public static bool Exists(Assembly assembly)
{
return System.IO.File.Exists(assembly.Location + ".config" )
}
}
Run Code Online (Sandbox Code Playgroud)
然后使用你想要的地方
ConfigurationManagerHelper.Exists(); // or pass assembly
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11230 次 |
| 最近记录: |