应用程序配置(app.config)

时间:2011-07-09 09:46:56

标签: .net c#-4.0 app-config

我正在从下面粘贴的代码中读取应用程序配置文件。我的问题是,如果配置文件中没有这些设置,我需要使用默认值编写缺少的条目或完整的配置文件。请帮我。提前致谢

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");
}

3 个答案:

答案 0 :(得分:1)

如果app.config中缺少的条目是执行所必需的,那么您当然应该提供正确的处理。提供默认值或停止执行并修复app.config。

答案 1 :(得分:1)

可以在此处找到更好的读取(和写入).config值的方法......

How to change App.config file run time using C#

希望有帮助吗?

答案 2 :(得分:0)

我所期望的是将缺少的条目写入配置文件的某种方式。或暗示这样做。如果丢失或者如果在混淆文件中缺少其中一个值,我是否设法找到一种方法来编写混淆文件

请找到以下粘贴的代码

            String _ProcessName = Process.GetCurrentProcess().ProcessName;
            String _ConfigarationFileName = _ProcessName + ".exe.config";
            String _ApplicationStartupPath = Application.StartupPath;

        if (!System.IO.File.Exists(System.IO.Path.Combine(_ApplicationStartupPath, _ConfigarationFileName)))
        {

            //Creating XmlWriter Settings
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            settings.IndentChars = "  ";
            settings.NewLineOnAttributes = true;

            //creating a empty xml documet to write the Configaration Key's 
            //If you are good in xml writing you can use  below code part to created the key values straight away.  
            using (XmlWriter writer = XmlWriter.Create(System.IO.Path.Combine(_ApplicationStartupPath, _ConfigarationFileName), settings))
            {   
                writer.WriteStartElement("configuration");
                writer.WriteStartElement("appSettings");
                writer.WriteEndElement();
                writer.WriteEndElement();                    
                writer.Flush();
            }
        }


        //This code part will create the keys and write the values for you. 
        //Open the Configaration file to Write the values 

        System.Configuration.Configuration config = 
            System.Configuration.ConfigurationManager.OpenExeConfiguration(System.IO.Path.Combine(_ApplicationStartupPath,_ProcessName + ".exe")); // you can enter either exe or dll if its a class library. 


        config.AppSettings.Settings.Add("TempServerName", ""); 
        config.AppSettings.Settings.Add("TempUserName", "");
        config.AppSettings.Settings.Add("TempPassword", "");


        config.Save(System.Configuration.ConfigurationSaveMode.Modified);

非常感谢你的所有回复。