C#中使用设置(Settings.settings) Properties.Settings.Default

应用程序及用户设置

在设计时创建新设置的步骤

在“Solution Explorer”(解决方案资源管理器)中,展开项目的“Properties”(属性)节点。

在“Solution Explorer”(解决方案资源管理器)中,双击要在其中添加新设置的 .settings 文件。此文件的默认名称是 Settings.settings。

在应用程序会话之间更改设置值

有时,在编译和部署了应用程序后,可能需要在应用程序会话之间更改设置值。例如,可能需要更改连接字符串,使其指向正确的数据库位置。由于设计时工具在应用程序编译和部署后不可用,所以必须手动在文件中更改设置值。

在应用程序会话间更改设置值的步骤


使用 Microsoft 记事本或其他某种文本或 XML 编辑器,打开与应用程序关联的 <AssemblyName>.exe.config 文件。(该文件在Bin文件夹下的Debug或Release文件夹下)


找到要更改的设置条目。它应看似以下示例:

<setting name="Setting" serializeAs="String">
            <value>这是设置值</value>
            </setting>
            

为设置键入新值,然后保存该文件。

在运行时使用设置

运行时应用程序可以通过代码使用设置。具有应用程序作用域的设置值能够以只读方式进行访问,而用户作用域设置的值可以进行读写。在 C# 中可以通过 Properties 命名空间使用设置。

在运行时读取设置

可在运行时使用 Properties 命名空间读取应用程序作用域及用户作用域设置。Properties 命名空间通过Properties.Settings.Default 对象公开了项目的所有默认设置。编写使用设置的代码时,所有设置都会出现在 IntelliSense 中并且被强类型化。因此,举例来说,如果设置的类型为 System.Drawing.Color,则无需先对其进行强制类型转换即可使用该设置,如下例所示:

this.BackColor = Properties.Settings.Default.myColor;

在运行时保存用户设置

应用程序作用域设置是只读的,只能在设计时或通过在应用程序会话之间修改 <AssemblyName>.exe.config 文件来进行更改。然而,用户作用域设置却可以在运行时进行写入,就像更改任何属性值那样。新值会在应用程序会话持续期间一直保持下去。可以通过调用 Settings.Save 方法来保持在应用程序会话之间对用户设置所做的更改。这些设置保存在 User.config 文件中。

在运行时写入和保持用户设置的步骤


访问用户设置并为其分配新值,如下例所示:

Properties.Settings.Default.myColor = Color.AliceBlue;
            

如果要保持在应用程序会话之间对用户设置所做的更改,请调用 Save 方法,如以下代码所示:

Properties.Settings.Default.Save();
时间: 2024-08-29 02:51:48

C#中使用设置(Settings.settings) Properties.Settings.Default的相关文章

Properties.Settings.Default

Settings里的属性根据范围分为两种 “用户”和“应用程序” 范围为“应用程序”的属性,读取都是从APP.CONFIG里获取,设置也可以通过手工修改App.config改变,但是在程序中无法对其进行赋值,只能读取. 而范围为“用户”的属性 Settings 在第一次运行时会读取App.config里的初始值 但是一旦调用Save方法后,Settings里“用户”范围的属性就会保存在系统里面,类似WebForm里的Cookies一样 从此以后,读取都会从系统里保存的值里读取,手工修改App.c

【转】The content of element type &quot;configuration&quot; must match &quot;(properties?,settings?,typeAliases?,typeHandlers?,objectFactory?...

[转]The content of element type "configuration" must match "(properties?,settings?,typeAliases?,typeHandlers?,objectFactory?... <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybat

The content of element type &quot;configuration&quot; must match &quot;(properties?,settings?,typeAliases?,typeHandlers?,objectFactory?...

今天在mybaties配置文件中碰到这样的一个问题:The content of element type "configuration" must match 解决如下:properties?,settings?,typeAliases?,typeHandlers?,objectFactory?,objectWrapperFactory?,plugins?,environments?,mappers? 按以上提示的顺序写标签.

【原创】mybatis配置文件报错:The content of element type &quot;configuration&quot; must match &quot;(properties&gt;settings&gt;typeAliases&gt;typeHa...

最近使用mybatis,在mybatis的xml配置文件中总是会提示错误 The content of element type "configuration" must match "(properties>settings>typeAliases>typeHandlers>objectFactory>objectWrapperFactory>plugins>environments>mappers?)". 后来发现

C# Properties.Settings.Default

使用Properties.Settings可以保存程序运行时更改的设置. 例如,可以保存最后一次使用的ID号,当程序再次启动的时候优先使用上一次使用的ID号. 读属性值: string DebugValue = Properties.Settings.Default.Debug; 更新属性值: Properties.Settings.Default.Debug = "vhuichen"; Properties.Settings.Default.Save();

[email&#160;protected]创建 userSettings/Microsoft.SqlServer.Configuration.LandingPage.Properties.Settings

System.Configuration.ConfigurationErrorsException: 创建 userSettings/Microsoft.SqlServer.Configuration.LandingPage.Properties.Settings 的配置节处理程序时出错: 未能加载文件或程序集“System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”或它的某一个依赖项.系统没找到指定的文

mybatis配置报错(properties?,settings?,typeAliases?,typeHandlers?,objectFactory?,objectWrapperFactory?,reflectorFactory?,plugins?,environments?,databaseIdProvider?,mappers?)

如下报错:解决方案:要按照提示的顺序添加属性,(properties?,settings?,typeAliases?,typeHandlers?,objectFactory?,objectWrapperFactory?,reflectorFactory?,plugins?,environments?,databaseIdProvider?,mappers?) java.lang.ExceptionInInitializerError at houji.dao.TaskModelMapperTes

当引用了Properties.Settings后,如果执行的时候,出现&quot;配置系统无法初始化&quot; 或者 某某节点不正确

自定义了一个 PowerConfig命名空间   PowerSettings.Settings 然后一个exe,引用了该dll,在app.cinfig里增加了配置项 <applicationSettings>    <PowerConfig.PowerSettings>      <setting name="SharedIp" serializeAs="String">        <value>1.1.1.1&l

知其然知其所以然--Properties.Settings.Default.Save();

winform提供自带的配置,最近查看了一下,数据到底保存到什么地方去了 主目录在这下面C:\Users\Administrator\AppData\                 使用的是win7的系统 C:\Users\Administrator\AppData\Local\程序的名字\BmpProject.exe_Url_2zmdfmzmrtwjomeciqta4ch2sieyx4id\1.0.0.0\user 最后一个user是一个xml config类型的 文件 <?xml vers