예제 #1
0
 /// <summary>
 /// 判断指定的数据库密码是否正确
 /// </summary>
 public static bool IsPassword(string password)
 {
     if (string.IsNullOrEmpty(password))
     {
         password = "******";
     }
     else
     {
         password = CommonServices.GetStringMd5(password);
     }
     try
     {
         LiteEngine liteEngine = new LiteEngine(_dataFile, password);
     }
     catch { return(false); }
     return(true);
 }
예제 #2
0
        /// <summary>
        /// 用指定的密码打开LiteDb数据库文件,如果不成功将返回Flase
        /// </summary>
        public static bool Open(string password)
        {
            try
            {
                _dataFile = $"{UserSettings.AppDataDir}\\RemoteItems.db";
                if (!File.Exists(_dataFile))//新建数据库文件
                {
                    File.WriteAllBytes(_dataFile, Resources.RemoteItems);
                }
                if (string.IsNullOrEmpty(password))
                {
                    password = "******";
                }
                else
                {
                    password = CommonServices.GetStringMd5(password);
                }
                //验证密码是否正确
                try { LiteEngine liteEngine = new LiteEngine(_dataFile, password); }
                catch { return(false); };

                _litedb = new LiteDatabase($"Filename={_dataFile};Password={password}");
                _litedb.Mapper.EmptyStringToNull = false;
                _litedb.Mapper.TrimWhitespace    = false;

                _tableDirectory      = _litedb.GetCollection <DbItemDirectory>("Directory");
                _tableRemoteLink     = _litedb.GetCollection <DbItemRemoteLink>("RemoteLink");
                _tableSetting_rdp    = _litedb.GetCollection <DbItemSettingRdp>("Setting_rdp");
                _tableSetting_ssh    = _litedb.GetCollection <DbItemSettingSsh>("Setting_ssh");
                _tableSetting_telnet = _litedb.GetCollection <DbItemSettingTelnet>("Setting_telnet");
                _tableSshHostKeys    = _litedb.GetCollection <DbSshHostKeys>("SshHostKeys");
                _tablePuttyColor     = _litedb.GetCollection <DbPuttyColor>("PuttyColor");

                #region 数据库升级

                if (_litedb.Engine.UserVersion < 1)
                {
                    //将原来的默认设置id="0001" 改成"default"
                    foreach (var item in _tableSetting_rdp.Find(m => m.Id == "0001"))
                    {
                        _tableSetting_rdp.Delete("0001");
                        _tableSetting_rdp.Insert("default", item);
                    }
                    foreach (var item in _tableSetting_ssh.Find(m => m.Id == "0001"))
                    {
                        _tableSetting_ssh.Delete("0001");
                        _tableSetting_ssh.Insert("default", item);
                    }
                    foreach (var item in _tableSetting_telnet.Find(m => m.Id == "0001"))
                    {
                        _tableSetting_telnet.Delete("0001");
                        _tableSetting_telnet.Insert("default", item);
                    }
                    _litedb.Engine.UserVersion = 1;
                }

                #endregion 数据库升级

                return(true);
            }
            catch (Exception e)
            {
                throw new Exception($"打开数据库文件失败。\n原因:{e.Message}");
            }
        }