【问题标题】:Application Configuration (app.config)应用程序配置 (app.config)
【发布时间】: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


    【解决方案1】:

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

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

    希望有帮助吗?

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

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

        请找到下面粘贴的代码

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

        非常感谢您的所有回复。

        【讨论】:

          猜你喜欢
          • 2012-02-24
          • 2014-05-30
          • 1970-01-01
          • 1970-01-01
          • 2014-10-06
          • 1970-01-01
          • 2010-11-07
          • 2011-12-13
          • 1970-01-01
          相关资源
          最近更新 更多