我定义了:
public static string Firstname { get; set; }
public static string Surname { get; set; }
public static bool DocBook5 { get; set; }
public static string Language { get; set; }
我将这些信息存储在App.config。
中然后我用它来加载和连接:
Firstname = ConfigurationManager.AppSettings["firstname"];
Surname = ConfigurationManager.AppSettings["surname"];
DocBook5 = Convert.ToBoolean(ConfigurationManager.AppSettings["docbook5"]);
Language = ConfigurationManager.AppSettings["language"];
但是,如果我使用Console.WriteLine发布属性内容,则看起来属性为空。 可以显示GetConfig类的完整代码there
也许有人知道为什么?
答案 0 :(得分:1)
正如我在评论中提到的那样,你正在阅读错误的部分,而不是从appsetting。如果您在配置中需要自定义部分,请点击此链接。http://haacked.com/archive/2007/03/12/custom-configuration-sections-in-3-easy-steps.aspx/否则将其移至应用部分
答案 1 :(得分:0)
查看您的App.config。 您存储的数据位于用户设置中,而不是 appSettings ,但您从 appSettings 获取数据。
您可以将所有数据放入appSettings,也可以定义 configSections ,在其中添加名称 UserSettings 。 (使用' GetSection '而不是' 的AppSettings &#39)
稍后,在代码中,您需要请求特定部分,并从那里获取数据。
编辑:如果您需要代码示例,请告诉我,以匹配您的App.config文件