I am using the vanilla library of keepass simply by adding the .exe file to my project. From my code I am accessing PwDatabase like so:
var dbpath = @"\\MyPath\To\PersonalVault\PersonalVault.kdbx";
var masterpw = "myPassword";
var ioConnInfo = new IOConnectionInfo { Path = dbpath };
var compKey = new CompositeKey();
compKey.AddUserKey(new KcpPassword(masterpw));
var db = new KeePassLib.PwDatabase();
db.Open(ioConnInfo, compKey, null);
From there I want to access and read the custom string field that I have previously manually added from the Advanced tab of entry on the UI:
var entry = db.RootGroup.FindEntry(new KeePassLib.PwUuid(KeePassLib.Utility.MemUtil.HexStringToByteArray("97A51FE92F700D4FB665DC6AA7C9D67D")), true);
var customString = entry.Strings.Where(lookingFor => lookingFor.Key.Equals("customString")).FirstOrDefault().Value;
However I end up with a null value here.
Any suggestion on how I can read this or is it simply not exposed through the vanilla KeePass.exe?
The "Entry" tab on KeePass.exe UI shows data stored in PwEntry (KeePass source code is downloadable here) with reserved key names. Reserved key names are shown in code sample below.
The "Advanced" tab on KeePass.exe UI stores strings with custom key names. So, when reading them in code, entry.Strings.ReadSafe("myCustomString") works (assuming that a custom string with that key name is present in the password entry). Below is the code that I tried and it works.
var pwUuid = new KeePassLib.PwUuid(KeePassLib.Utility.MemUtil.HexStringToByteArray("F3A8DC6E93571944B485AA947C68FB5E"));
var entry = db.RootGroup.FindEntry(pwUuid, true);
// Reserved key names ("Entry" tab on KeyPass.exe)
Console.WriteLine($"Title : {entry.Strings.ReadSafe("Title")}");
Console.WriteLine($"UserName: {entry.Strings.ReadSafe("UserName")}");
Console.WriteLine($"Password: {entry.Strings.ReadSafe("Password")}");
Console.WriteLine($"URL : {entry.Strings.ReadSafe("URL")}");
Console.WriteLine($"Notes : {entry.Strings.ReadSafe("Notes")}");
// Custom key names ("Advanced" tab on KeePass.exe)
Console.WriteLine($"myCustomString: {entry.Strings.ReadSafe("myCustomString")}");
This also made me wonder about clashes between reserved and custom key names. As expected KeePass.exe UI does show an error if I try to create a custom key with a reserved key name.