Okay, so a while ahead I posted how to read other config files of other programs (here is the link Previous Post. I managed to do it. But now there is another problem. 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. If the path points to a file in the same directory, the program works perfect! But if it points to a file outside the directory it throws an exception of type System.NullReferenceException.
Here is the code
private void saveBtn_Click(object sender, EventArgs e)
{
try
{
// save everything and close
string attachment = attachTxtBox.Text;
var configMap = new ExeConfigurationFileMap { ExeConfigFilename = configFileName };
// it throws exception here when
// the path points to a file outside the exes directory
Configuration externalConfig = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
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: " + ex.Message);
Application.Exit();
}
}
The content of email.config is:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings file="">
<clear />
<add key="ServerAddress" value="" />
<add key="Port" value="" />
<add key="Sender" value="" />
<add key="SenderPassword" value="" />
<add key="Subject" value="" />
<add key="AttachmentPath" value="" />
<add key="Body" value="" />
</appSettings>
</configuration>
What am I doing wrong here?
EDIT: The value of configFileName is "email.config"