开发者

Application Configuration (app.config)

开发者 https://www.devze.com 2023-03-19 05:26 出处:网络
I\'m reading an application configuration file form the code pasted below. My question is if this these setting are not available in the configuration file I need to write the missing entries or the c

I'm reading an application configuration file form the code pasted below. My question is if this these setting are not available in the configuration file I need to write the missing entries or the complete configuration file with the default values. Please help me. Thanks in advance

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)_Configu开发者_高级运维rationAppSettings.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");
}


A better wayof reading (and writing) the .config values can be found here...

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

Hope that helps?


If the missing entry in the app.config is necessary for execution, then of course you should provide proper handling. Either provide default values or stop execution and fix the app.config.


What i expected was some way to write the missing entries to the configuration file. or a hint of doing that.how ever i have managed to find a way to write the configaration file if missing or if one of the value is missing in the configaration file

please find the below pasted code

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

Thank you very much all of your replies.

0

精彩评论

暂无评论...
验证码 换一张
取 消