title: Dynamically creating App.config with connection string if App.config does not exists in Application-Data folder
body: <p>I need to do my code the following task. I wrote some code as below, but I am getting error while setting connection string. Looks like it does not have set property.</p>
  
  <ol>
  <li><p>Check if App.config is exists in user’s “Application Data” folder.</p></li>
  <li><p>If does NOT exists then create App.config  with ConnectionString</p></li>
  <li><p>If does exists   then check if it was connection string or not, and if missing then add connection string.</p>
  
  <pre><code>string SomeConnectionString ="My Connection String goes here"
  
  //Checking whether App.config file exits in "Application Data" folder or not
    String appDataPath = Environment.GetFolderPath
                     (Environment.SpecialFolder.LocalApplicationData);
  
    if (!File.Exists(appDataPath + "App.config"))
      {
         appDataPath = Path.Combine(appDataPath , "App.config");
         Configuration config = ConfigurationManager.OpenExeConfiguration
                                (appDataPath );
         var setting = config.ConnectionStrings.ConnectionStrings
                       ["MyConnectionString"]; 
  
         if (setting == null) 
         { 
             Configuration Config = ConfigurationManager.OpenExeConfiguration
                                    (ConfigurationUserLevel.None);
             config.ConnectionStrings.ConnectionStrings
              ["MyConnectionString"].ConnectionString = SomeConnectionString; 
               //I am getting error on line above. Looks like there is not set 
               //property on it
  
          Config.Save(ConfigurationSaveMode.Modified,true);
          ConfigurationManager.RefreshSection("connectionStrings");
        }
  
      }
      else
      {
          //check whether it has Connection string or not
          //if not then add connection string 
      }
  </code></pre></li>
  </ol>