سؤال

I started adding settings to my C# project in VS2010, by adding rows in the Project -> Properties -> Settings tab. A Settings.settings file (with Settings.Designer.cs file) was automatically generated under Properties.

But when I try to access some settings in the code like ConfigurationManager.AppSettings["NameOfSetting"] it always returns null; AppSettings.Count is always 0. Furthermore I checked the bin folder and when I build the project, there is no MyProjectName.exe.config file or app.config file there.

What might have wrong with the VS project? How do I force it to create the .exe.config file like it's supposed to?

UPDATE:

I completely removed/deleted the app.config and Settings files from my project then re-added one setting in the project properties, to re-create it from scratch. When I built the project again, this time a ProjectName.exe.config file is in the \bin folder. But my code gets the same results: ConfigurationManager.AppSettings["MySetting"] is null and AppSettings.Count = 0.

Here is the auto-generated app.config file, in the project folder, which is identical to the Project.exe.config file created in a build:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
        <section name="MyDarnProject.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
</configSections>
<applicationSettings>
    <MyDarnProject.Properties.Settings>
        <setting name="MySetting" serializeAs="String">
            <value>21937058</value>
        </setting>
    </MyDarnProject.Properties.Settings>
</applicationSettings>
</configuration>

I notice that the element is named applicationSettings, not appSettings as expected. And the setting itself is totally different, with a element and child element, instead of <add key="MySetting" value="21937058"/> like in others' examples. I'm using Visual Studio Professional 2010, Version 10.0.40219.1 SP1Rel

The Settings.Designer.cs code has everything exactly as in an example given below, except that there's no set to go with the get but I don't think that's a big deal.

[global::System.Configuration.ApplicationScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("21937058")]
public long MySetting {
    get {
        return ((long)(this["MySetting"]));
    }
}

MyProject.Properties.Settings.Default.MySetting does access the setting value! So I guess I may have to do it that way; I'm going to consider that a workaround since I still have no clue why the project isn't building the ProjectName.exe.config file the way every other project I've ever made does.

هل كانت مفيدة؟

المحلول

Get it from this property:

YourAppNamespace.Properties.Settings.Default.NameOfSetting

You can easily see how to reference the values if you take a closer look at the Settings.Designer.cs file:

namespace YourAppNamespace.Properties {


    [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")]
    internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {

        private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));

        public static Settings Default {
            get {
                return defaultInstance;
            }
        }

        [global::System.Configuration.UserScopedSettingAttribute()]
        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.Configuration.DefaultSettingValueAttribute("asas")]
        public string NameOfSetting {
            get {
                return ((string)(this["NameOfSetting"]));
            }
            set {
                this["NameOfSetting"] = value;
            }
        }
    }
}

نصائح أخرى

Make sure you have a section in your App.config file

here is an example you can follow

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="somesetting" value="7"/>
    <add key="somelocation"  value="abc.txt"/>
  </appSettings>
</configuration>

you read the above using code shown below

string configvalue1 = ConfigurationManager.AppSettings["somesetting"];
string configvalue2 = ConfigurationManager.AppSettings["somelocation"];

if you want to access the Properties within the Settings you can do this

Properties.Settings.Default.<Your_Setting_Name>
var value = ConfigurationManager.AppSettings["NameOfSetting"]

Will look for <appSettings> section in App.config file

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="NameOfSetting" value="Foo"/>
  </appSettings>
</configuration>

To access settings in Settings.settings file use (Visual Studio will help you resolve namespace of Settings class- that is one in Properties folder of your project). Settings class will have strongly-typed properties for all rows that you have added to Project -> Properties -> Settings tab:

var value = Settings.Default.NameOfSetting;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top