Skip to main content
 首页 » 编程设计

c#之使用前确保 App.config 存在

2024年02月20日25wayfarer

<分区>

Possible Duplicate:
how to determine whether app.config file exists

有没有办法在尝试访问值之前确保 App.config 存在?我不想在执行此操作时遇到“对象引用未设置到对象错误实例”的情况:

machineName = appConfig.AppSettings.Settings["MachineName"].Value;

如果找不到我的 App.config,我不确定如何知道它是否存在或 appConfig 对象是否已正确填充。我试过了

if (appConfig.AppSettings.Settings.Count == 0) {//做某事 }

但我不确定这是否是最好的方法。