1

ANSWER for this question thanks to Jeremy C.:

There is no KeePass nuget package for the Net5.0 yet. Thats why there is that error message. Thanks Jeremy C. for the help and answers.

QUESTION:

Im getting this error after starting my solution.

Unhandled exception. System.TypeLoadException: Could not load type 'System.Drawing.Color' from assembly 'Splat, Version=3.0.0.0, Culture=neutral, PublicKeyToken=null'.

Already used google and tried to find a fix for it and also red all articles about similiar errors like "System.Drawing.Font" or "System.Drawing.Image". But theres nothing really helpful and nothing really informative about 'System.Drawing.Color'.

Ive got the code example and package from here: github.com/wismna/ModernKeePassLib

This is my code:

.csproj

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="ModernKeePassLib" Version="2.45.1" />
    <PackageReference Include="System.Drawing.Common" Version="5.0.2" />
  </ItemGroup>

</Project>

And:

using ModernKeePassLib;
using ModernKeePassLib.Interfaces;
using ModernKeePassLib.Keys;
using ModernKeePassLib.Serialization;
using System;
using System.Linq;
using System.Text;

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

            var dbpath = @"C:\Users\prusinma\Desktop\KeePassDatabase\Database.kdbx";
            var keypath = @"C:\Users\prusinma\Desktop\KeePassDatabase\Database.key";
            var masterpw = "1234abcd";

            Console.WriteLine("init done");

            byte[] DBPathBytes = Encoding.ASCII.GetBytes(dbpath);
            byte[] KeyPathBytes = Encoding.ASCII.GetBytes(keypath);

            var ioConnection = IOConnectionInfo.FromByteArray(DBPathBytes);

            var compositeKey = new CompositeKey();
            compositeKey.AddUserKey(new KcpPassword(masterpw)); // Password
            compositeKey.AddUserKey(new KcpKeyFile(IOConnectionInfo.FromByteArray(KeyPathBytes))); // Keyfile

            var db = new PwDatabase();
            db.Open(ioConnection, compositeKey, new NullStatusLogger());

            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"),
                             URL = entry.Strings.ReadSafe("URL"),
                             Notes = entry.Strings.ReadSafe("Notes")
                         };

            db.Save(new NullStatusLogger());
            var contents = db.IOConnectionInfo.Bytes;

            string bitString = BitConverter.ToString(contents);
            Console.WriteLine(bitString);

            Console.WriteLine(kpdata.ToString());
        }
    }
}
Beardy
  • 163
  • 1
  • 15
  • What version are you working in? – Gertjan Brouwer Jul 29 '21 at 10:30
  • Probably this is your problem: https://stackoverflow.com/a/47227347/5612691 – Gertjan Brouwer Jul 29 '21 at 10:31
  • @ gertjan if you mean NET version, its 5.0. Yeah ive seen that question already and also used the microsoft docs page to check out if "system.drawing" is supported in 5.0. https://learn.microsoft.com/en-us/dotnet/api/system.drawing.color?view=net-5.0 and according to the list on the bottom it should be supported – Beardy Jul 29 '21 at 10:35
  • It says you are using an assembly called splat? Where in the code do you include System.Drawing.Color? And where do you use it? – Gertjan Brouwer Jul 29 '21 at 10:41
  • no where, the posted code above is the only code i have. Its simply getting passwords out of a password manager tool. I also thought that maybe the solution/project is not clean, so i copied the code, created a new solution and pasted the code. Still same issue – Beardy Jul 29 '21 at 10:44
  • In the package: https://github.com/wismna/ModernKeePassLib/blob/master/ModernKeePassLib/Utility/GfxUtil.cs . You can see #if !KeePassUAP then it includes System.Drawing. Otherwise it won't. I suspect your KeePassUAP is True and therefore does not include them correctly. Not sure how to set KeePassUAP yet – Gertjan Brouwer Jul 29 '21 at 10:46
  • I see, i have literally no clue what the GfxUtil.cs is for. I just want to simply display the passwords in my console :/ – Beardy Jul 29 '21 at 10:55
  • I don't know how to fix it at this point. Maybe you can use another Nuget? – Gertjan Brouwer Jul 29 '21 at 11:47
  • I used yesterday the old version of the KeePassLib nuget. Which is mentioned in this [Question](https://stackoverflow.com/questions/4680352/store-sensitive-information-inside-keepass-database-from-c-sharp) but the KeePassLib is outdated and doesnt work in NET 5.0. The 2021 version is "ModernKeePass" which im using now. As you can see i used the code from the old question and adapted it for the 2021 version one – Beardy Jul 29 '21 at 11:59
  • The creator of the ModernKeePass gave an example here https://github.com/wismna/ModernKeePassLib which i use in mine currenlty. You can see it when you scroll down to "Usage". I hope i didnt do anything wrong in my code. – Beardy Jul 29 '21 at 12:01

1 Answers1

1

Those classes were moved into their own nuget package. Add it to your project and you should be good to go: https://www.nuget.org/packages/System.Drawing.Common/

From the project directory at the command line:

dotnet add package System.Drawing.Common

Closer inspection reveals ModernKeepPass targets.netstandard1.2 and will not work with 5's System.Drawing nuget package without being upgraded to target the new framework.

https://github.com/wismna/ModernKeePassLib/blob/master/ModernKeePassLib/ModernKeePassLib.csproj

<PropertyGroup>
  <TargetFramework>netstandard1.2</TargetFramework>
Jeremy C.
  • 74
  • 4
  • Hey thanks for your suggestion. I added the command line in my project which uses the nuget package `ModernKeePass` and i get the same error message. But as mentioned in the comments above, this error message is propably caused by the nuget package and not my own solution. I guess the creator of the nuget package `ModernKeePass` has to update his code am right? Or do you see another possible fix for it? – Beardy Jul 29 '21 at 15:17
  • @Beardy can you share your csproj file? I have a project successfully using this package, so I can compare the two. If your project uses a package A that needs a package B, and your project includes that package B, the first package A should now have all of it dependencies met at runtime. – Jeremy C. Jul 29 '21 at 15:26
  • ill add it in the question. Added – Beardy Jul 29 '21 at 15:27
  • I see it, and it looks like what I expected. It's odd that the error says it's looking in the Splat assembly... did the error change at all after you added the reference? Here's the other project's csproj file: https://github.com/wismna/ModernKeePassLib/blob/master/ModernKeePassLib/ModernKeePassLib.csproj – Jeremy C. Jul 29 '21 at 15:33
  • are you also using net 5.0? – Beardy Jul 29 '21 at 15:34
  • I am, but I'm using it directly instead of using a package that uses it. – Jeremy C. Jul 29 '21 at 15:37
  • hm how can i change that? I started to code 2 months ago. So sorry for those stupid questions. – Beardy Jul 29 '21 at 15:38
  • Just clone the repo at https://github.com/wismna/ModernKeePassLib (git clone https://github.com/wismna/ModernKeePassLib.git .), add the package reference, and build it. Then you just have to figure out how to get your project to use that new assembly. – Jeremy C. Jul 29 '21 at 15:40
  • What is your project doing, while using that package? I want simply get only the password out of the KeePass database. Just read/get one password for an automation test – Beardy Jul 29 '21 at 15:40
  • I'm just using System.Drawing to get the dimensions of an uploaded image. – Jeremy C. Jul 29 '21 at 15:42
  • Any chance you could upload your solution? Of course you can delete your important files. But it would make everything easier when i could compare :) – Beardy Jul 29 '21 at 15:48
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/235421/discussion-between-jeremy-c-and-beardy). – Jeremy C. Jul 29 '21 at 15:52
  • I cloned the repo, added the `` into the csproj file and it throws an error tho. Curious now why its working for ya, when we have the same repos – Beardy Jul 29 '21 at 15:53