使用配置文件(.settings、.config)存储应用程序配置

引言

我不知大家早先是如何保存应用程序配置,以备下次打开时使用的,反正我开始学.Net的时候就去研究序列化,以二进制或XML格式的序列化来保存应用程序配置。这样每次都要建立单独的配置类,并书写读写配置代码,相当麻烦。

期间也看了看.config文件的读写方式,感觉还是很麻烦,不如自己序列化来的踏实。

后来才猛然发现微软早提供好了settings,用以定义.config文件内容,并生成相应的强类型类,使用起来极其方便~,感觉就像在火星居住了半辈子突然发现还有地球这好地方*_*~

鉴于网上settings方面的资料不多、不详,我就以此文来介绍一下基本的用法,让小鸟们少走我那条破路,老鸟请直接从上空滑翔而过即可。



基本使用方法

首先建立一个WinForm项目,设计这样一个窗体:



通过“添加新项”功能添加一个MyApp.settings:




在打开的数据表格中做如下设置:



这样就设置好了a、b、c三个不同类型的配置变量,现在就可以在程序中使用它们了:
  1. private void Form1_Load(object sender, EventArgs e)

  2. {

  3.     numericUpDown1.Value = MyApp.Default.a;

  4.     textBox1.Text = MyApp.Default.b;

  5.     checkBox1.Checked = MyApp.Default.c;

  6. }



  7. private void Form1_FormClosing(object sender, FormClosingEventArgs e)

  8. {

  9.     MyApp.Default.a = numericUpDown1.Value;

  10.     MyApp.Default.b = textBox1.Text;

  11.     MyApp.Default.c = checkBox1.Checked;

  12.     MyApp.Default.Save();

  13. }
复制代码
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Portainer的配置文件settings.json的内容如下: { "app": { "analytics": false, "hide_labels": false, "logo": "", "logo_dark": "", "logo_mini": "", "page_size": 10, "snapshot_interval": 0, "ssl": false, "ssl_cert": "", "ssl_key": "", "template_upload": true, "theme": "", "title": "", "update_notification": true }, "auth": { "ldap": { "enabled": false, "debug": false, "config_file": "", "url": "", "starttls": false, "tls_ca_cert": "", "tls_client_cert": "", "tls_client_key": "", "tls_client_insecure_skip_verify": false, "search_dn": "", "search_password": "", "search_filter": "", "group_search_dn": "", "group_search_filter": "", "group_search_attribute": "", "group_membership_attribute": "", "group_dn_attribute": "", "group_filter_enabled": false, "group_filter": "", "auto_create_users": true, "auto_create_admin": true, "user_attribute_mappings": {}, "group_attribute_mappings": {}, "debug_bind": false, "debug_search": false }, "oauth": { "enabled": false, "debug": false, "config_file": "", "client_id": "", "client_secret": "", "scopes": "", "auth_url": "", "token_url": "", "api_url": "", "callback_url": "", "auto_create_users": true, "auto_create_admin": true, "user_attribute_mappings": {} }, "azuread": { "enabled": false, "debug": false, "config_file": "", "client_id": "", "client_secret": "", "tenant_id": "", "scopes": "", "auth_url": "", "token_url": "", "api_url": "", "callback_url": "", "auto_create_users": true, "auto_create_admin": true, "user_attribute_mappings": {} }, "jwt": { "enabled": false, "debug": false, "secret": "thisisasecret", "algorithm": "HS256", "expiration": 86400, "header_name": "Authorization", "header_prefix": "Bearer" } }, "data": { "data_location": "", "sync_interval": 60, "templates_url": "https://templates.portainer.io", "templates_check_for_update": true, "registry_credentials": [], "extensions": [], "extensions_check_for_update": true, "azure_credentials": [], "tls_ca_certificates": [], "tls_client_certs": [], "tls_client_keys": [] }, "endpoints": { "azure": [], "docker": [], "edge": [] }, "integrations": { "agent": { "enabled": true, "endpoint": "", "public_url": "", "tls": false, "tls_ca_cert": "", "tls_client_cert": "", "tls_client_key": "" }, "azuremonitor": { "enabled": false, "debug": false, "config_file": "", "subscription_id": "", "client_id": "", "client_secret": "", "tenant_id": "", "log_analytics_workspace_id": "", "log_analytics_workspace_key": "", "metrics": { "enabled": true, "interval": 60, "retention": 30, "metrics": [ { "type": "cpu", "aggregation": "Average", "dimension": "ContainerName" }, { "type": "memory", "aggregation": "Average", "dimension": "ContainerName" }, { "type": "network", "aggregation": "Total", "dimension": "ContainerName" } ] }, "logs": { "enabled": true, "interval": 60, "retention": 30, "log_template": "{\"time\": \"{{.Timestamp}}\", \"level\": \"{{.Level}}\", \"message\": \"{{.Message}}\", \"container\": \"{{.ContainerName}}\", \"image\": \"{{.ImageName}}\", \"instance\": \"{{.InstanceID}}\"}" } }, "cicd": { "enabled": false, "debug": false, "config_file": "", "gitlab": { "base_url": "", "token": "", "auto_deploy_on_push": true, "auto_deploy_on_merge_request": false, "auto_deploy_on_tag": false, "auto_deploy_on_release": false, "auto_remove_stacks": false }, "github": { "base_url": "", "token": "", "auto_deploy_on_push": true, "auto_deploy_on_pull_request": false, "auto_deploy_on_tag": false, "auto_deploy_on_release": false, "auto_remove_stacks": false }, "bitbucket": { "base_url": "", "username": "", "app_password": "", "auto_deploy_on_push": true, "auto_deploy_on_pull_request": false, "auto_deploy_on_tag": false, "auto_deploy_on_release": false, "auto_remove_stacks": false } } }, "labels": [], "registries": [], "settings_version": 2, "snapshot": { "schedule": "", "keep": 5 }, "telemetry": { "enabled": true } } 这是一个JSON格式的文件,包含了Portainer的各种配置项,例如应用程序的设置、认证方式、数据存储位置、终端节点、集成等等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值