Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# Configuration Files

C# Configuration Files

Scheduled Pinned Locked Moved C#
helpcsharpsecurityquestionworkspace
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    staticv
    wrote on last edited by
    #1

    I am encountering a problem while reading config files. The scenario is like this, I have two programs. Program A reads its configuration from a config file and program B is only used to modify the contents of the config file which A reads. The name of the config file is email.config. It is in the same directory in which program A & B resides. The problem is that I get path of a file for attachment using open file dialog in program B. If the path points to a file in the same directory, program B works perfect while saving the data back to the config file! But if it points to a file outside the directory it throws an exception of type System.NullReferenceException. I have placed comments in the code where the exception occurs. Here is the code

    static readonly string configFileName = "email.config";

    private void saveBtn_Click(object sender, EventArgs e)
    {
    try
    {
    // save everything and close
    string serverAddr = serverAddressTxtBox.Text;
    string port = portTxtBox.Text;
    bool ssl = sslRadioYes.Checked == true ? true : false;
    string senderAddr = senderTxtBox.Text;
    string password = passwordTxtBox.Text.Length != 0 ? Encrypt(passwordTxtBox.Text) : "";
    string subject = subjectTxtBox.Text;
    string attachment = attachTxtBox.Text;
    string messageBody = msgBodyTxtBox.Text;
    var configMap = new ExeConfigurationFileMap { ExeConfigFilename = configFileName };
    Configuration externalConfig = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);

    	/\* If attachment points to a file outside the exes directory
    	externalConfig doesn't reads the config properly
    	and throws exception of type System.NullReferenceException \*/
    	
    	// here the exception is thrown
    	externalConfig.AppSettings.Settings\["ServerAddress"\].Value = serverAddr;
    	externalConfig.AppSettings.Settings\["Port"\].Value = port;
    	externalConfig.AppSettings.Settings\["SSL"\].Value = ssl.ToString();
    	externalConfig.AppSettings.Settings\["Sender"\].Value = senderAddr;
    	externalConfig.AppSettings.Settings\["SenderPassword"\].Value = password;
    	externalConfig.AppSettings.Settings\["Subject"\].Value = subject;
    	externalConfig.AppSettings.Settings\["AttachmentPath"\].Value = attachment;
    	externalConfig.AppSettings.Settings\["Body"\].Value = messageBody;
    
    		// Save values in config
    	externalConfig.Save(ConfigurationSaveMode.Full);
    	Application.Exit();
    }
    catch (System.Exception ex)
    {
    	MessageBox.Show("Error: " +
    
    0 S 2 Replies Last reply
    0
    • S staticv

      I am encountering a problem while reading config files. The scenario is like this, I have two programs. Program A reads its configuration from a config file and program B is only used to modify the contents of the config file which A reads. The name of the config file is email.config. It is in the same directory in which program A & B resides. The problem is that I get path of a file for attachment using open file dialog in program B. If the path points to a file in the same directory, program B works perfect while saving the data back to the config file! But if it points to a file outside the directory it throws an exception of type System.NullReferenceException. I have placed comments in the code where the exception occurs. Here is the code

      static readonly string configFileName = "email.config";

      private void saveBtn_Click(object sender, EventArgs e)
      {
      try
      {
      // save everything and close
      string serverAddr = serverAddressTxtBox.Text;
      string port = portTxtBox.Text;
      bool ssl = sslRadioYes.Checked == true ? true : false;
      string senderAddr = senderTxtBox.Text;
      string password = passwordTxtBox.Text.Length != 0 ? Encrypt(passwordTxtBox.Text) : "";
      string subject = subjectTxtBox.Text;
      string attachment = attachTxtBox.Text;
      string messageBody = msgBodyTxtBox.Text;
      var configMap = new ExeConfigurationFileMap { ExeConfigFilename = configFileName };
      Configuration externalConfig = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);

      	/\* If attachment points to a file outside the exes directory
      	externalConfig doesn't reads the config properly
      	and throws exception of type System.NullReferenceException \*/
      	
      	// here the exception is thrown
      	externalConfig.AppSettings.Settings\["ServerAddress"\].Value = serverAddr;
      	externalConfig.AppSettings.Settings\["Port"\].Value = port;
      	externalConfig.AppSettings.Settings\["SSL"\].Value = ssl.ToString();
      	externalConfig.AppSettings.Settings\["Sender"\].Value = senderAddr;
      	externalConfig.AppSettings.Settings\["SenderPassword"\].Value = password;
      	externalConfig.AppSettings.Settings\["Subject"\].Value = subject;
      	externalConfig.AppSettings.Settings\["AttachmentPath"\].Value = attachment;
      	externalConfig.AppSettings.Settings\["Body"\].Value = messageBody;
      
      		// Save values in config
      	externalConfig.Save(ConfigurationSaveMode.Full);
      	Application.Exit();
      }
      catch (System.Exception ex)
      {
      	MessageBox.Show("Error: " +
      
      0 Offline
      0 Offline
      0x3c0
      wrote on last edited by
      #2

      What object is null? Is it externalConfig, externalConfig.AppSettings or configMap? What format is your external path in? Have you escaped the backslashes correctly in your external path?

      Between the idea And the reality Between the motion And the act Falls the Shadow

      S 1 Reply Last reply
      0
      • 0 0x3c0

        What object is null? Is it externalConfig, externalConfig.AppSettings or configMap? What format is your external path in? Have you escaped the backslashes correctly in your external path?

        Between the idea And the reality Between the motion And the act Falls the Shadow

        S Offline
        S Offline
        staticv
        wrote on last edited by
        #3

        The externalConfig.AppSettings.Settings collection is null, it doesn't have any keys loaded.

        Top Web Hosting Providers[^] Do, or do not. There is no 'try'.

        1 Reply Last reply
        0
        • S staticv

          I am encountering a problem while reading config files. The scenario is like this, I have two programs. Program A reads its configuration from a config file and program B is only used to modify the contents of the config file which A reads. The name of the config file is email.config. It is in the same directory in which program A & B resides. The problem is that I get path of a file for attachment using open file dialog in program B. If the path points to a file in the same directory, program B works perfect while saving the data back to the config file! But if it points to a file outside the directory it throws an exception of type System.NullReferenceException. I have placed comments in the code where the exception occurs. Here is the code

          static readonly string configFileName = "email.config";

          private void saveBtn_Click(object sender, EventArgs e)
          {
          try
          {
          // save everything and close
          string serverAddr = serverAddressTxtBox.Text;
          string port = portTxtBox.Text;
          bool ssl = sslRadioYes.Checked == true ? true : false;
          string senderAddr = senderTxtBox.Text;
          string password = passwordTxtBox.Text.Length != 0 ? Encrypt(passwordTxtBox.Text) : "";
          string subject = subjectTxtBox.Text;
          string attachment = attachTxtBox.Text;
          string messageBody = msgBodyTxtBox.Text;
          var configMap = new ExeConfigurationFileMap { ExeConfigFilename = configFileName };
          Configuration externalConfig = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);

          	/\* If attachment points to a file outside the exes directory
          	externalConfig doesn't reads the config properly
          	and throws exception of type System.NullReferenceException \*/
          	
          	// here the exception is thrown
          	externalConfig.AppSettings.Settings\["ServerAddress"\].Value = serverAddr;
          	externalConfig.AppSettings.Settings\["Port"\].Value = port;
          	externalConfig.AppSettings.Settings\["SSL"\].Value = ssl.ToString();
          	externalConfig.AppSettings.Settings\["Sender"\].Value = senderAddr;
          	externalConfig.AppSettings.Settings\["SenderPassword"\].Value = password;
          	externalConfig.AppSettings.Settings\["Subject"\].Value = subject;
          	externalConfig.AppSettings.Settings\["AttachmentPath"\].Value = attachment;
          	externalConfig.AppSettings.Settings\["Body"\].Value = messageBody;
          
          		// Save values in config
          	externalConfig.Save(ConfigurationSaveMode.Full);
          	Application.Exit();
          }
          catch (System.Exception ex)
          {
          	MessageBox.Show("Error: " +
          
          S Offline
          S Offline
          staticv
          wrote on last edited by
          #4

          Well, I configured it out myself after debugging for almost 5 hours, Damn! The problem was when I used OpenFileDialog to get the file path, it changed the current directory to the one which is selected in the dialog, so the program couldn't find the config file. All I did was to set the RestoreDirectory property of OpenFileDialog to true and *poof* it worked

          Top Web Hosting Providers[^] Do, or do not. There is no 'try'.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups