Search code examples
c#linqkeepass

CS1061 'IEnumerable<<anonymous type: x>>' does not contain a definition for dump


I just want to get the password out of my software KeePass.

After using the code from an old question here Link to the question, im getting this error message:

S1061 'IEnumerable<<anonymous type: string Group, string Title, string Username, string Password>>' does not contain a definition for 'Dump' and no accessible extension method 'Dump' accepting a first argument of type 'IEnumerable<<anonymous type: string Group, string Title, string Username, string Password>>' could be found (are you missing a using directive or an assembly reference?) KeePasso C:\Users\prusinma\source\repos\KeePasso\KeePasso\Program.cs 36 Active

This is the code im using:

using System.Linq;
using KeePassLib;
using KeePassLib.Keys;
using KeePassLib.Serialization;

namespace KeePasso
{
    class Program
    {
        static void Main()
        {

            var dbpath = @"\\xxx\Home_VIE\xxx\Desktop\KeePassDatabase\Database.kdbx";
            var keypath = @"\\xxx\Home_VIE\xxx\Desktop\KeePassDatabase\Database.key";
            var masterpw = "1234abcd";

            var ioConnInfo = new IOConnectionInfo { Path = dbpath };
            var compKey = new CompositeKey();
            compKey.AddUserKey(new KcpPassword(masterpw));
            compKey.AddUserKey(new KcpKeyFile(IOConnectionInfo.FromPath(keypath))); // Keyfile

            var db = new PwDatabase();
            db.Open(ioConnInfo, compKey, null);

            var kpdata = from entry in db.RootGroup.GetEntries(true)
                         select new
                         {
                             Group = entry.ParentGroup.Name,
                             Title = entry.Strings.ReadSafe("Title"),
                             Username = entry.Strings.ReadSafe("UserName"),
                             Password = entry.Strings.ReadSafe("Password"),
                         };

            kpdata.Dump(); // this is how Linqpad outputs stuff
            db.Close();
        }
    }
}

In the last rows in the code, there is a red underline at Dump. Which displays the same error message i shared above.

I was already trying to find similiar questions and in most of them they were related to the type. But as i can see, all the datas/entries in Title, Username and Password are strings.

Would appreciate if someone could help me here out. Id also be open for a other solution how to read out the password from the database.

Thanks!


Solution

  • Since kpdata is collection of anonimous types, which has overriden ToString (and if entry.Strings.ReadSafe returns string or some type with "correctly" overriden ToString method) you can just use Console.WriteLine on it:

    Console.WriteLine(string.Join(Environment.NewLine, kpdata));; // instead of kpdata.Dump();
    

    Otherwise you will need to find a way to import LINQPad's Dump method into your project or just use some json serialization library to convert object to string.