Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Winform / C# / SQL

I get this error when I try to run my program:
The key 'SecurityKey' does not exist in the appSettings configuration section.

More accurately the program runs and the login form appears but when I attempt to login it tries to access a "Key" in app.config page but it can't seem to find it.

This is the line that is looking for the Key:
string key = (string)settingsReader.GetValue("SecurityKey", typeof(String));

I'll mention that when I build and run the program it runs fine but if I try to run it from the .exe in the \bin\Debug folder I get the above mentioned error.

I believe it's referring to one of my classes:
<appsettings>
<add key="SecurityKey" value="Key goes here">
<add key="ClientSettingsProvider.ServiceUri" value="">


I found on a site someone that had the same error and they found the problem was that XML is case sensitive and the person had <appsettings> instead of <appsettings>
and that worked for him but I went through my application and I couldn't find any similar problem.

Am I not supposed to use the .exe in the deb \bin\Debug folder perhaps?

Any help is much appreciated.

~ Chris
Posted

1 solution

In your project you should have a app.config file with a section similar to this:

XML
<applicationSettings>
    <WindowsFormsApplication1.Properties.Settings>
        <setting name="SecurityKey" serializeAs="String">
            <value>Hello</value>
        </setting>
    </WindowsFormsApplication1.Properties.Settings>
</applicationSettings>




Auto-generated files called Settings.settings and Settings.Designer.cs should have been created.

They will allow you to access the setting like this:

C#
private void button1_Click(object sender, EventArgs e)
{
    String Security = WindowsFormsApplication1.Properties.Settings.Default.SecurityKey;
}



If you go to Project, Properties, Settings you will see a button that says Synchronize. Clicking that sometimes gets everything refreshed (when things get funky).
 
Share this answer
 
v3
Comments
Sandeep Mewara 27-Mar-11 2:44am    
My 5!
Sandeep Mewara 27-Mar-11 2:44am    
Comments from OP:
Thanks I'll keep that step in mind for the future but when I tried that it said this:

No user.config files were found in any of the following locations:
and it listed 8 different paths to the directories on my computer:

4 in C:\Documents and Settings\Chris\Application Data\....path to myapp.vshost.exe_Url_AWholeBunchOfNumbers\1.0.0.0

4 in C:\Documents and Settings\Chris\Local Settings\Application Data....path to myapp.vshost.exe_Url_AWholeBunchOfNumbers\1.0.0.0

When I try to navigate to the paths I find that they don't completely exist.
I find that the "path to myapp" above does not exist.

I'm a little new to C# so I wonder if I'm doing something fundementally wrong like not deploying this project properly.

Any other thoughts?

Many thanks,

~ Chris
Steve Wellens 27-Mar-11 10:14am    
Those are user settings files as opposed to application settings (user settings are per user, application settings are application wide). However, when I do it I only get four files.

Is it possible you have two applications and are putting the settings in one application but trying to read them out of the other application settings file?
cbgriz 27-Mar-11 12:17pm    
It's all in one single VS project and all in the same folder so I don't believe so but I can't completey dismiss that possibility as I got the some parts of my code from other projects and incorporated it into this one.

The thing is that this works when I run if from VS (ctrl+F5). It's when I try to run the .exe (from bin/debug) that I get the error. One thing I did was run it from VS after commenting it out of my app.config file and ran it from VS again. Low and behold I got the error. I uncommented it again and the error never came back.

It's almost like when I run the .exe it's not including my app.config file.

I even tried adding a new app.config file (Add New Item / Select the Application Configuration File ... etc) and doing so noticed that instead of app.config it was named App.config. I placed my code in here and wondering if that could be the problem...unfortunately it wasn't.

Thanks for trying if any other thoughts I would appreciate it,

~ Chris

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900