如何使用 .cfg 和程序的保存选项 [英] How to use .cfg and saving options of a program

查看:24
本文介绍了如何使用 .cfg 和程序的保存选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在用 vb 做一个项目,我想要一个选项表单,用户可以在其中自定义控件(例如导入图片和标签),并且我希望将它保存在外部 .cfg 或其他方法中.附言我仍然是这方面的新手,随着项目的进展,我将继续提出更简单的问题.

解决方案

您会想要写入 My.Settings 并使用 My.Settings.Save 保存它们>.

首先,您需要在项目的属性页中定义您的设置:

user 设置为应用程序在其下运行的每个用户帐户 创建新设置,application 设置使得设置全局并影响每个人,无论他们在系统中拥有什么权限.

保存设置的方式也存在差异:

应用程序:保存在[项目名称].config文件

用户: 保存在 \\[LocalSettings\]ApplicationData\\

I am currently doing a project in vb and I want to have an option form where a user can customize the controls (e.g. importing pictures and labels) and I want it to be saved in an external .cfg or another method. P.S. I am still a newbie at this and I will continue to ask more simple questions as I progress on it project.

解决方案

You'll be wanting to write to My.Settings and saving them with My.Settings.Save.

First you'll want to define your settings in the project's property pages:

A user setting creates new settings for each user-account the application is run under, application setting makes it so that the settings are global and affects everyone regardless of what privileges they hold in the system.

There are also differences to how the settings are saved:

Application: Saves in the [project name].config file

User: Saves in the <c:\Documents>\<username>\[LocalSettings\]ApplicationData\<companyname>\<appdomainname>_<eid>_<hash>\<version>

You can see more here on where the settings are saved.

Example use-cases:

Application:

  • License key (see the 'word of warning' below)
  • Administrative settings (proxy settings, filters, etc)

User:

  • Username
  • Time/Date of last use
  • Application colour scheme

In this example I'll use these settings:

If I wanted to set the setting Username, I would want to do:

My.Settings.Username = "Test"

and to save the settings you would do

My.Settings.Save()

The output file is [project name].config, and the settings stored in it are similar to this:

<userSettings>
    <ConsoleApplication1tst.My.MySettings>
        <setting name="PictureLocations" serializeAs="String">
            <value />
        </setting>
        <setting name="Labels" serializeAs="String">
            <value />
        </setting>
        <setting name="Username" serializeAs="String">
            <value />
        </setting>
        <setting name="LastAccessDate" serializeAs="String">
            <value />
        </setting>
    </ConsoleApplication1tst.My.MySettings>
</userSettings>

A word of warning:

Never place passwords or license keys in the application's settings as these can be easily read. If you have to, encrypt them first (and don't use MD5 for it either!).

More information on My.Settings can be found here.

这篇关于如何使用 .cfg 和程序的保存选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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