Windows窗体应用程序设置的问题

时间:2010-05-21 16:03:34

标签: c# .net visual-studio

我已经使用Visual Studio为Windows窗体应用程序中的应用程序设置生成了一个类。调试时,应用程序设置不会填充值。为什么没有价值观?

以下是生成的代码:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.1
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace Unidata_Client.Properties {


    [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "10.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("blah")]
        public string UnidataUsername {
            get {
                return ((string)(this["UnidataUsername"]));
            }
            set {
                this["UnidataUsername"] = value;
            }
        }

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

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

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

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

这是app.config文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="Unidata_Client.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <userSettings>
        <Unidata_Client.Properties.Settings>
            <setting name="UnidataUsername" serializeAs="String">
                <value>blah</value>
            </setting>
            <setting name="UnidataPassword" serializeAs="String">
                <value>blah </value>
            </setting>
            <setting name="UnidataHost" serializeAs="String">
                <value>blah</value>
            </setting>
            <setting name="UnidataPort" serializeAs="String">
                <value>123</value>
            </setting>
            <setting name="UnidataAccount" serializeAs="String">
                <value>blah</value>
            </setting>
        </Unidata_Client.Properties.Settings>
    </userSettings>
</configuration>

以下是我使用设置的方法:

 using (var session = UniObjects.OpenSession(Settings.Default.UnidataHost,
                int.Parse(Settings.Default.UnidataPort),
                Settings.Default.UnidataUsername,
                Settings.Default.UnidataPassword,
                Settings.Default.UnidataAccount,
                "udcs"))
            {
                .....
            } 

2 个答案:

答案 0 :(得分:6)

您正在使用“usersettings”(设计师中的“用户”范围) 更改(非默认)时,这些设置将存储在您的个人资料中 这些设置会覆盖应用程序目录中的设置
您是否可以尝试将设置更改为设计器中的“应用程序”范围并再次运行?
如果您有值,那么您可以确保在配置文件目录中覆盖用户设置。

在C:\ Users \ XX \ AppData \ Local \ ApplicationName \ Version \

中查找文件user.config

答案 1 :(得分:0)

您的app.config是否被复制并重命名为调试输出目录中的MyApp.exe.config?