zoukankan      html  css  js  c++  java
  • VS C#开发中WinForm中Setting.settings的作用

    1、定义 
    在Settings.settings文件中定义配置字段。把作用范围定义为:User则运行时可更改,Applicatiion则运行时不可更改。可以使用数据网格视图,很方便;
    
    2、读取配置值
    
    text1.text = Properties.Settings.Default.FieldName;
    //FieldName是你定义的字段
    
    3、修改和保存配置
    
    Properties.Settings.Default.FieldName = "server";
    
    Properties.Settings.Default.Save();//使用Save方法保存更改
    
    4、也可以自己创建
    
    创建一个配置类FtpSetting。在WinForm应用程序里,一切配置类都得继承自 ApplicationSettingsBase 类。
    
    sealed class FtpSettings : ApplicationSettingsBase
    
    {
    [UserScopedSetting]
    [DefaultSettingValue("127.0.0.1")]
    public string Server
    {
    get { return (string)this["Server"]; }
    set { this["Server"] = value; }
    }
    [UserScopedSetting]
    [DefaultSettingValue("21")]
    public int Port
    {
    get { return (int)this["Port"]; }
    set { this["Port"] = value; }
    }
    }
    
    
    使用上述配置类,可以用:
    
    private void button2_Click(object sender, EventArgs e)
    {
    FtpSettings ftp = new FtpSettings();
    string msg = ftp.Server + ":" + ftp.Port.ToString();
    MessageBox.Show(msg);
    }
    
    
    我们在使用上述FtpSetting 配置时,当然要先进行赋值保存,然后再使用,后面再修改,再保存,再使用。
    private void button2_Click(object sender, EventArgs e)
    {
    FtpSettings ftp = new FtpSettings();
    ftp.Server = "ftp.test.com";
    ftp.Port = 8021;
    ftp.Save();
    ftp.Reload();
    string msg = ftp.Server + ":" + ftp.Port.ToString();
    MessageBox.Show(msg);
    }
    嗯。已经Save了,你可能会在应用程序文件夹里找不到它到底保存到哪里去了。由于我们是用UserScope的,所以其实该配置信息是保存到了你的Windows的个人文件夹里去了。比如我的就是 C:Documents and SettingsrooksLocal SettingsApplication DataTestWinForm目录了。
    C:UsersAdministratorAppDataLocalMicrosoftRecentlyUsedFiles.vshost._Url_sdz00nt2violg5f0j24k0kgd0rncwteq1.0.0.0
  • 相关阅读:
    配置禅道遇到的那些事儿
    HDU 1181
    HDU1016
    HDU 1518
    2015长春区域赛赛后总结
    Codeforces Round #322 (Div. 2) A B C
    Codeforces Round #325 (Div. 2) A B
    Codeforces Round #324 (Div. 2) A B
    CSU 1530
    CSU 1535
  • 原文地址:https://www.cnblogs.com/ps122/p/5746467.html
Copyright © 2011-2022 走看看