Ejemplo n.º 1
0
        public static void UpdateKeePassEntry(PwEntry entry,
            string username = "",
            string title = "",
            string url = "",
            string notes = "",
            string password = "",
            string[] attachments = null,
            string[] tags = null)
        {
            UpdateValue(entry, "UserName", username);
            UpdateValue(entry, "Title", title);
            UpdateValue(entry, "URL", url);
            UpdateValue(entry, "Notes", notes);
            UpdateValue(entry, "Password", password);

            if(tags != null)
            {
                var tagsCopy = entry.Tags.ToArray();
                foreach(var tag in tagsCopy)
                    entry.RemoveTag(tag);

                foreach (var tag in tags)
                    entry.AddTag(tag);
            }

            if (attachments != null)
            {
                var fileNames = attachments.Select(o => Path.GetFileName(o)).ToList();
                var binaries = entry.Binaries.CloneDeep();
                var filesToAdd = new List<string>();

                foreach(var binary in binaries)
                {
                    entry.Binaries.Remove(binary.Key);
                }

                foreach(var attachment in attachments)
                {
                    var fileName = Path.GetFileName(attachment);

                    var bytes = File.ReadAllBytes(attachment);
                    if(bytes != null)
                    {
                        var protectedBytes = new ProtectedBinary(true, bytes);
                        entry.Binaries.Set(fileName, protectedBytes);
                    }
                }
            }
        }