Exemplo n.º 1
0
        public KeePassLib.PwGroup getGroup(string name)
        {
            KeePassLib.PwGroup group = new KeePassLib.PwGroup();

            var ioconninfo = new KeePassLib.Serialization.IOConnectionInfo();

            if (!(string.IsNullOrEmpty(KeepassDBFilePath)))
            {
                ioconninfo.Path = base64Decode(KeepassDBFilePath);
                KeePassLib.Keys.CompositeKey compkey = new KeePassLib.Keys.CompositeKey();
                if (string.IsNullOrEmpty(KeepassKeyFilePath) && string.IsNullOrEmpty(KeepassMasterPassword))
                {
                    throw new Exception("A Key file or Master Password has not been set!");
                }
                else
                {
                    if (!(string.IsNullOrEmpty(KeepassKeyFilePath)))
                    {
                        compkey.AddUserKey(new KeePassLib.Keys.KcpKeyFile(base64Decode(KeepassKeyFilePath)));
                    }
                    if (!(string.IsNullOrEmpty(KeepassMasterPassword)))
                    {
                        compkey.AddUserKey(new KeePassLib.Keys.KcpPassword(base64Decode(KeepassMasterPassword)));
                    }
                    var db = new KeePassLib.PwDatabase();

                    try
                    {
                        db.Open(ioconninfo, compkey, null);
                        KeePassLib.Collections.PwObjectList <KeePassLib.PwGroup> groups = db.RootGroup.GetGroups(true);

                        group = groups.First(i => i.Name == name);
                    }
                    catch
                    {
                        throw;
                    }
                    finally
                    {
                        if (db.IsOpen)
                        {
                            db.Close();
                            db = null;
                        }
                    }
                }
            }
            else
            {
                throw new Exception("Keepass DB Path has not been set!");
            }

            return(group);
        }