Clickonce应用程序未发现AppSettings [英] Clickonce application not seeting AppSettings

查看:117
本文介绍了Clickonce应用程序未发现AppSettings的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有完全信任的,仅在线的单击一次应用程序,该应用程序依赖于App.config-> appSettings集合中的设置.

I have a Full trust, online only, click-once application that relies on settings in the App.config --> appSettings collection.

发布后,首次执行该应用程序时,将读取设置,并且一切正常.如果我关闭该应用程序并再次运行它,则appSettings中的项目不再存在,并且appSettings.Count等于0.

Once published, the first time the application is executed, the settings are read and everything works fine. If I close the application and run it again, the items that are in appSettings are no longer there and appSettings.Count is equal to 0.

我已将app.config设置为内容",并设置为始终复制".

I have set the app.config as "Content" and to "Copy Always".

任何想法都可能导致appSettings中的项目消失吗?或解决这个问题的方法?

Any Idea what could be causing the items in appSettings to disappear? or a way to get around this?

此外,请注意,设置仅在读取时用于确定应用程序的运行方式.我们不是要操纵设置.

Also, please note that the settings are only being read, and being used to determine how the application is run. We're not trying to manipulate the settings.

推荐答案

我猜您使用的设置错误.

I guess you are using the settings wrong.

首先,您需要注意应用程序和用户设置之间的区别. 应用程序设置是恒定的,不能从您的代码中更改(例如,默认值,连接字符串等). 用户设置用于在应用程序运行时更改的设置,通常是因为您的应用程序中存在某种设置"对话框.

First of all, you need to mind the difference between application and user settings. Application settings are constant and can not be changed from your code (for example default values, connection strings, etc.). User settings are for settings that change when the application runs, usually because there is some sort of Settings dialog in your application.

第二,您需要正确访问它们.人们倾向于(而且我不知道为什么)使用诸如ConfigurationManager之类的东西或其他东西来访问设置.在应用程序和用户设置中使用Properties.Settings.Default.SettingName更加容易,而且更不容易出错.

Secondly, you need to access them properly. People tend to (and I don't know why) use things like ConfigurationManager or other stuff to access settings. It is far easier and less error-prone to use Properties.Settings.Default.SettingName for both application and user settings.

更改用户设置的内容如下:

Changing a user setting would read like this:

Properties.Settings.Default.SettingName = settingValue;
Properties.Settings.Default.Save();

Save的调用很重要,否则更改将不会保留.

The call to Save is important, as otherwise the change will not be persisted.

第三:无需更改项目中包含app.config的方式.默认情况下,它将重命名为applicationname.exe.config,并且除非您另有说明,否则它将始终包含在您的输出中. ClickOnce安装也是如此:默认情况下将包括它.如果您玩这个游戏或app.config的ClickOnce部署设置,实际上可能会破坏事情!将它们还原为默认值,然后不理会它们.

Thirdly: No need to change the way app.config is included in your project. By default it will be renamed to applicationname.exe.config and will always be included in your output unless you say otherwise. The same goes for the ClickOnce installation: It will be included by default. You may actually break things if you play with this or the ClickOnce deployment settings for app.config! Revert them to default and leave them alone.

现在,更新设置会丢失设置并恢复为默认值的一种情况.在这种情况下,您将必须从先前安装的版本迁移设置,否则用户设置将被安装中的默认值覆盖.迁移设置的步骤是:

Now one case where the settings will be lost and reverted to defaults is when you update your application. In this case you will have to migrate your settings from the previously installed version, otherwise the user settings will be overridden with the default values from the installation. What you do to migrate the settings is:

在设置设计器中使用默认值true定义一个名为SettingsUpgradeRequired的用户设置(名称并不重要-应该是不言自明的).然后,在应用程序启动时,执行以下操作:

Define a user setting named SettingsUpgradeRequired (name doesn't really matter - should be self-explaining, however) with a default value of true in the settings designer. Then, when your application starts, you do this:

if (Properties.Settings.Default.SettingsUpgradeRequired)
{
    Properties.Settings.Default.Upgrade();
    Properties.Settings.Default.SettingsUpgradeRequired = false;
    Properties.Settings.Default.Save();
}

这可确保仅在需要时才进行设置的迁移. SettingsUpgradeRequired 仅会在首次安装(在这种情况下Upgrade不执行任何操作)和更新应用程序后为true,因为-正如我所说-默认情况下, ClickOnce安装将覆盖以前的配置,直到您执行升级.

This makes sure a migration of the settings only occurs when required. SettingsUpgradeRequired will only be true upon the first installation (in which case Upgrade does nothing) and after an update of your application, because - as I said - by default the configuration from the ClickOnce installation will override the previous configuration until you perform the upgrade.

这篇关于Clickonce应用程序未发现AppSettings的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆