views:

2550

answers:

5

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"

+2  A: 

Are you accessing the file by its full path or just the file name?

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

UPDATE

It looks as though things are more complicated than I first thought and this doesn't seem to be the issue here - see the comments. The line that's raising the exception is:

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

So that means that there's a null reference somewhere along the chain. If you can identify which it is then that should give you a pointer to the problem.

ChrisF
I'm accessing the config file by just the file name because it is in the same directory.
hab
Well if it's in a different folder you'll need to access it via it's full path.
ChrisF
I'm not accessing the file, I'm just storing its path in a key in the config file. Please read my code and I have commented the place where exception occurs.
hab
Without seeing the value of configMap I can't really think of anything else.
ChrisF
The value of configFileName is "email.config"
hab
That's my point. If it's in a different folder it needs to be something along the lines of: "C:\Folder1\Folder2\email.config"
ChrisF
With just the filename the code will look in the current folder (i.e. the folder where the exe is running) and not find it. Hence the exception.
ChrisF
But the config file is in the same directory. The file which I get from other OpenFileDialog isn't used to open the config file. I just store it inside the config file.
hab
I'm confused now, though I think we're talking about different things here. OK - what's the value stored in the config file?
ChrisF
Sorry, yes I know it is confusing. The file path which I get from OpenFileDialog is to be stored in the config file.
hab
Looking back at your code I'm not sure which line is throwing exception. Is it the creation of "configMap" or when it's used?
ChrisF
And it's the full path that's stored?
ChrisF
Can you clarify which line is raising the exception?
ChrisF
It is this lineexternalConfig.AppSettings.Settings["ServerAddress"].Value = serverAddr;
hab
A: 

What code are you using to get the FileName and Path back from the OpenFileDialog.

Is it a fully qualified path to the file ?

e.g.

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

By the sounds of it, whats being saved is only a filename, so your application is only looking in the current path.

Eoin Campbell
Well, I think you interpreted me wrong. What I'm saying that the config file is in the same directory and I'm able to access it. The problem occurs when the file path which I get from the OpenFileDialog is outside the directory of the exe. But, but the file path which I get from the dialog is a full path and I don't use that file path. All I do is just store it in a key. But I cant do that
hab
A: 

I had the same problem, I don't know if this can help you but when I changed the name of the config file, which was in another folder as in your case, to .config and it didn't crash anymore, in my case I could change the name so I didn't continue the investigation how to make it work with other name, but of course I would like to know.

pablito
Thanks for helping but my config file already have '.config'
hab
A: 

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

Thanks everyone, ChrisF, Eoin Campbell and pablito.

hab
you *figured* it out ;-) sorry couldn't resist :)
fretje
A: 

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

Config file does not contain "SSL"

just my 2c for those that are trying this code.