コード例 #1
0
        /// <summary>
        /// 加载最近保存的登录信息
        /// </summary>
        /// <returns>上次是否保存密码</returns>
        private bool LoadRecentHistory()
        {
            mConnHises = new Dictionary <string, TConnHis>();
            //object oMyConfig = ConfigurationSettings.GetConfig("Historys"); //same as next
            HistoryConfSection historys = (HistoryConfSection)System.Configuration.ConfigurationManager.GetSection("Historys");
            bool bSavePassword          = historys.SaveInfo;

            foreach (HistoryElement his in historys.Historys)
            {
                try
                {
                    var sHost = his.Host.Trim();
                    mConnHises.Add(sHost, new TConnHis {
                        sHost = sHost, sUser = his.User, sPassword = his.Password
                    });
                }
                catch (ArgumentException ex)
                {
                }
            }
            return(bSavePassword);
        }
コード例 #2
0
        private void SaveRecentHistory()
        {
            bool bSaveInfo = chkSavePassword.Checked;

            Configuration      config   = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            HistoryConfSection historys = (HistoryConfSection)config.GetSection("Historys");

            historys.SaveInfo = bSaveInfo;
            historys.Historys.Clear();

            if (bSaveInfo)
            {
                try
                {
                    var sHost     = tboxHost.Text.Trim();
                    var sUser     = tboxUser.Text.Trim();
                    var sPassword = tboxPassword.Text;
                    var newHis    = new TConnHis {
                        sHost = sHost, sUser = sUser, sPassword = sPassword
                    };
                    if (mConnHises.ContainsKey(sHost))
                    {
                        mConnHises[sHost] = newHis;
                    }
                    else
                    {
                        mConnHises.Add(sHost, newHis);
                    }
                }
                catch (ArgumentException ex)
                {
                }

                //System.Configuration.ConfigurationManager;
                //save history
                int i = 0;
                foreach (var item in mConnHises)
                {
                    var his = new HistoryElement();
                    his.Host               = item.Value.sHost;
                    his.User               = item.Value.sUser;
                    his.Password           = item.Value.sPassword;
                    historys.Historys[i++] = his;
                }
                Debug.Print("write rec:{0}", i);
                try
                {
                    config.Save(ConfigurationSaveMode.Modified);
                }
                //catch (System.Configuration.ConfigurationErrorsException ex)
                //{
                //    FrmMain.PromptError(ex.Message);
                //}
                catch (Exception ex)
                {
                    FrmMain.PromptError(MyRes.Err_WritePriv);
                }
            }
            else if (mConnHises.Count > 0)
            {
                //清空列表
                mConnHises.Clear();
                tboxHost.AutoCompleteCustomSource = null;
                try
                {
                    config.Save(ConfigurationSaveMode.Modified);
                }
                catch (Exception ex)
                {
                    FrmMain.PromptError(MyRes.Err_WritePriv);
                }
            }
        }