如何判断app.config文件是否存在 [英] how to determine whether app.config file exists

查看:45
本文介绍了如何判断app.config文件是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在不使用File.Exists"的情况下找出 app.config 文件是否存在?我试过了

Is there a way to find out whether an app.config file exists, without using "File.Exists"? I tried

if ( !ConfigurationManager.ConnectionStrings.ElementInformation.IsPresent )
{...}

但是即使存在带有连接字符串的 app.config,IsPresent 也是假的.

but IsPresent is false even if app.config with a connection string exists.

我是否误解了 IsPresent 属性?

Did I misinterpret the IsPresent Property?

推荐答案

AppDomain 报告它期望应用程序的配置文件驻留的位置.您可以测试该文件是否确实存在(无需虚拟 AppSettings,也无需尝试找出应调用的配置文件或它所在的位置):

The AppDomain reports on where it expects the configuration file for the application to reside. You can test if the file actually exists (with no need for dummy AppSettings, and no need to try and work out what the configuration file should be called, or where it is located):

public static bool CheckConfigFileIsPresent()
{
   return File.Exists(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
}

这篇关于如何判断app.config文件是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆