开发者

.NET Application Settings -> setting the null string

What should I type (in both cases) in my app.config's applicationSettings section so that, when I read Settings, I can get the following:

  1. Settings.Default.FooString == null
  2. Settings.Default.FooString == string.Empty

? I guess there is no way to achieve both situations, or am I missing something?

Diffrerenciating whether string value is null or empty is unfortunately required, because this value goes to different system, that takes diffrent decisions upon these two. thanks


further info:

Settings.Designer.cs is being regenerated each time you modify the settings tab. The sample generated setting is:

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

the problem is with generated [DefaultSettingValueAttribute("")]. It works as I want when I correct it by hand with [DefaultSettingValueAttribute(null)], however, I would need to do this after whenever I modify any thing on the settings tab, which is unacceptable.

any开发者_StackOverflow中文版 clues?


You're right, there is no way to achieve both situations, because null != String.Empty.

But you could use Boolean String.IsNullOrEmpty(String) to check for both situations.


For a null, you simply omit the whole <value /> element.

For a string.Empty you use <value /> or <value></value>.


leave the appSetting undefined if you want null, use value="" if you want String.Empty

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜