C # configuration files

Well, thatโ€™s why on the eve I published how to read other configuration files of other programs (here is the link Previous post . I succeeded. There is one more problem: the scenario is this: I have two programs. Program A reads its configuration from the configuration file, and program B it is used only to modify the contents of the configuration file that A reads. The name of the configuration file is email.config, which is located in the same directory in which program A and B > reside.

The problem is that I get the file path for the attachment using the file open dialog. If the path points to a file in the same directory, the program works fine! 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(); } } 

Email.config content:

 <?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"

+5
c # file config
source share
5 answers

Well, I thought about it after debugging for almost 5 hours, Damn!

The problem was that I used OpenFileDialog to get the path to the file, it changed the current directory to the one selected in the dialog box, so the program could not find the configuration file. All I did was set the RestoreDirectory OpenFileDialog property to true and the poof in which it worked.

Thanks to everyone, ChrisF, Eoin Campbell and pablito.

+5
source share

Is the file accessed by its full path or simply by the file name?

If the latter, then this will work if the file is in the same folder as the executable file, but not otherwise.

UPDATE

It seems that things are more complicated than I thought, and this does not seem to be a problem - see comments. The line that throws the exception:

 externalConfig.AppSettings.Settings["ServerAddress"].Value = serverAddr; 

Thus, this means that the chain has a null reference. If you can determine what exactly, this should point to a pointer to the problem.

+2
source share

What code do you use to return the filename and path from OpenFileDialog.

Is this the full path to the file?

eg.

 openFileDialog1.FileName; //Contains "C:\\Path\\To\\The\\File.txt" 

By the sounds of this that is saved, this is only the file name, so your application only looks at the current path.

0
source share

I had the same problem, I donโ€™t know if this will help you, but when I changed the name of the configuration file, which was in a different folder, as in your case, to .config, and it no longer crashed, in my case I could would change the name, so I did not continue the investigation of how to make it work with a different name, but, of course, I would like to know.

0
source share
 externalConfig.AppSettings.Settings["SSL"].Value = ssl.ToString(); 

The configuration file does not contain "SSL"

just my 2c for anyone trying this code.

0
source share

All Articles