简体   繁体   中英

File (or assembly) can't be found after changing project's .NET target-framework from 4.0 to 3.5

I want to change my C# project, which is based on the .NET 4.0 to 3.5. So I changed the project's target-framework to 3.5.

After re-opening and trying to compile the project I get the error:

The file or assembly "System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" or one of its dependencies couldn't be found. The system can't find the file. oder eine Abhängigkeit davon wurde nicht gefunden. Das System kann die angegebene Datei nicht finden.

This happens in the file Resource.resx, which looks like that:

<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    <data name="traktor_connected" type="System.Resources.ResXFileRef, System.Windows.Forms">
        <value>..\Resources\traktor_connected.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
    </data>
    <data name="traktor_not_connected" type="System.Resources.ResXFileRef, System.Windows.Forms">
        <value>..\Resources\traktor-not-connected.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
    </data>

That reference is pointing to .Net 4 - you need to remove all .net 4 references and then readd them using the .net 3.5 version of the assemly and then it will work

paul

Changing the Target framework setting is not exactly perfect. In a C# project for example you have to remove the reference to Microsoft.CSharp by hand. And yes, it doesn't automatically update the System.Drawing assembly references in the .resx files. This is quite a flaw, works okay when you increase the .NET version number (what just about everybody normally does), kaboom if you try to decrease it. .NET is only forward compatible.

You'll have to remove the resources and add them back after changing the target framework. Pretty painful, editing the .resx file by hand to minimize the job is however possible. Right-click the .resx file, Open With and select the text editor. Change the Version for the assembly name from 4.0.0.0 to 2.0.0.0.

Beware however that there can be trickier resources, like ImageList, the kind whose data got embedded in the .resx in binary. You'll see a blob of base64 encoded bytes in the .resx file. That also has the version number embedded, done so by BinaryFormatter. The possible reason that VS doesn't do this for you. Writing a little program that decodes that base64 string back to byte[], locates the proper 0x34 and changes it to 0x32 and converts it back to base64 is only a hacker's delight, man over machine style :) Best to just re-add those to avoid the almost inevitable oopses.

I had this error when I installed a COM component in NET 4.5 then downgraded to NET 4.0. Removing and adding the reference made no difference.

I found this helpful.

* Solution or Workaround Change the default Platform Target CPU to allow applications to run on 64-bit machines. *

http://forums.arcgis.com/threads/19335-COM-Exception-was-unhandled-and-Class-not-registered

The NET 4.0 target environment for me is 32bit, so I get the Target CPU down to x86 in the properties window. Now it runs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM