【发布时间】:2011-07-10 12:20:08
【问题描述】:
我正在从下面粘贴的代码中读取应用程序配置文件。我的问题是,如果这些设置在配置文件中不可用,我需要使用默认值编写缺少的条目或完整的配置文件。请帮我。在此先感谢
String _ProcessName = Process.GetCurrentProcess().ProcessName;
String _ConfigarationFileName = _ProcessName + ".exe.config";
String _ApplicationStartupPath = Application.StartupPath;
System.Configuration.AppSettingsReader _ConfigurationAppSettings = new System.Configuration.AppSettingsReader();
if (System.IO.File.Exists(System.IO.Path.Combine(_ApplicationStartupPath, _ConfigarationFileName)))
{
try
{
String COMMsDB = (String)_ConfigurationAppSettings.GetValue("TempDatabaseName", Type.GetType("String")); // 'Temp database name
String COMMsServer = (String)_ConfigurationAppSettings.GetValue("TempServerName", Type.GetType("String")); // 'Temp server name
String CDBUserName = (String)_ConfigurationAppSettings.GetValue("TempUserName", Type.GetType("String")); // 'Temp server user id
String CDBPass = (String)_ConfigurationAppSettings.GetValue("TempPassword", Type.GetType("String")); // 'Temp server encrypted password
String COMMsPCName = (String)_ConfigurationAppSettings.GetValue("TempComputerName", Type.GetType("String")); // 'Temp Pc Name for execute
String SP1Name = (String)_ConfigurationAppSettings.GetValue("SP1Name", Type.GetType("String")); // 'Stored procedure one name to execute before starting the transfer process
String SP2Name = (String)_ConfigurationAppSettings.GetValue("SP2Name", Type.GetType("String")); // 'Stored procedure two name to execute before starting the Sales process
String SP3Name = (String)_ConfigurationAppSettings.GetValue("SP3Name", Type.GetType("String")); // 'Stored procedure three name to execute before starting the TransferOrders process
String SP4Name = (String)_ConfigurationAppSettings.GetValue("SP4Name", Type.GetType("String")); // 'Stored procedure four name to execute before starting the Database Copy process
String SP5Name = (String)_ConfigurationAppSettings.GetValue("SP5Name", Type.GetType("String")); // 'Stored procedure four name to execute before Exitting the application
}
catch (Exception ex)
{
MessageBox.Show("Please Check the Configaration File " + ex.Message);
}
}
else
{
MessageBox.Show("Configaration File is Missing");
}
【问题讨论】:
标签: .net c#-4.0 app-config