I use app.config to store some info. My file looks like this:
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="używajDomyślnie" value="false" />
<add key="sInicjały" value="ŁM" />
<add key="nPrzedstawiciel" value="Łukasz Motyczka" />
<add key="nPozycja" value="Przedstawiciel Naukowo-Handlowy" />
<add key="nTelefon" value="+48 784 567 670" />
<add key="nEmail" value="motyczka.lukasz@bmgrp.pl" />
<add key="dataoferty" value="" />
<add key="ostatniNrOferty" value="" />
</appSettings>
<connectionStrings>
<add name="Oferty_BMGRP.Properties.Settings.BazaDanychConnectionString" connectionString="Data Source=|DataDirectory|\BazaDanych.sdf" providerName="Microsoft.SqlServerCe.Client.3.5" />
</connectionStrings>
<system.windows.forms jitDebugging = "true"/>
</configuration>
The snippet that throws a NullexceptionError is:
if(checkBox1.CheckState == CheckState.Checked)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
AppSettingsSection app = config.AppSettings;
//app.Settings.Clear();
//app.Settings.Add("używajDomyślnie", "true");
//app.Settings.Add("sInicjały", sInicjaly.ToString());
//app.Settings.Add("nPrzedstawiciel", nPrzedstawiciel.ToString());
//app.Settings.Add("nPozycja", nPozycja.ToString());
//app.Settings.Add("nTelefon", nTelefon.ToString());
//app.Settings.Add("nEmail", nEmail.ToString());
config.AppSettings.Settings["używajDomyślnie"].Value = "true"; //<- THIS LINE GIVES ME AN ERROR
config.AppSettings.Settings["sInicjały"].Value = sInicjaly.ToString();
config.AppSettings.Settings["nPrzedstawiciel"].Value = nPrzedstawiciel.ToString();
config.AppSettings.Settings["nPozycja"].Value = nPozycja.ToString();
config.AppSettings.Settings["nTelefon"].Value = nTelefon.ToString();
config.AppSettings.Settings["nEmail"].Value = nEmail.ToString();
config.Save(ConfigurationSaveMode.Modified);
form1.checkBox1.CheckState = CheckState.Checked;
}
When I debug it or build release in VS it works fine. Also it works on my laptop. But on every other machine I have null exception error. Also it worked until I tried to make a setup project (which works but meesed up this one). Whatmore the jit Debugging does not work, I still have a .net error (continue or exit).
Also I did notice that on my computer and some other laptop I was using, when error occurs it shows:
System.NullReferenceException: Odwołanie do obiektu nie zostało ustawione na wystąpienie obiektu.
w Oferty_BMGRP.Form3.button1_Click(Object sender, EventArgs e) w C:\Users\user\Documents\Visual Studio 2010\Projects\BMGRP\Oferty BMGRP\OfertyBMGRP\Form3.cs:wiersz 282
Sorry for polish, but what I want to point is that on both machines it shows path to same folder, which exists only on one of them.
Please help :)
EDIT!
I just found out that if I build the release. Run application and make th above code run, it creates MyApplication.exe.config file, and then app works fine on every computer. How can I make it to work as setup project, where .exe.config is not created, only app.config?