Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# &引用;无法加载文件或程序集System.Drawing或其依赖项之一;在.Net 2.0、VS2010和Windows 8上出错_C#_Visual Studio 2010_.net 2.0_Windows 8 - Fatal编程技术网

C# &引用;无法加载文件或程序集System.Drawing或其依赖项之一;在.Net 2.0、VS2010和Windows 8上出错

C# &引用;无法加载文件或程序集System.Drawing或其依赖项之一;在.Net 2.0、VS2010和Windows 8上出错,c#,visual-studio-2010,.net-2.0,windows-8,C#,Visual Studio 2010,.net 2.0,Windows 8,我在Windows窗体应用程序项目上收到FileNotFoundException,并显示以下消息: 要复制此问题,请执行以下操作: 选择新建、项目,选择.Net Framework 2.0作为目标,并选择Windows窗体应用程序作为项目类型 在默认情况下创建的表单的属性上,选择图标属性的值。任何.ico文件都可以。这将在resx文件中嵌入该文件 编译并运行应用程序 当我这样做时,程序停在this.Icon=((System.Drawing.Icon)(resources.GetObjec

我在Windows窗体应用程序项目上收到FileNotFoundException,并显示以下消息:

要复制此问题,请执行以下操作:

  • 选择新建、项目,选择.Net Framework 2.0作为目标,并选择Windows窗体应用程序作为项目类型
  • 在默认情况下创建的表单的属性上,选择图标属性的值。任何.ico文件都可以。这将在resx文件中嵌入该文件
  • 编译并运行应用程序
当我这样做时,程序停在
this.Icon=((System.Drawing.Icon)(resources.GetObject($this.Icon)))行上但以下情况除外:

System.IO.FileNotFoundException was unhandled
  Message=Could not load file or assembly 'System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
  Source=mscorlib
  FileName=System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
我是在最近安装在Windows8开发者预览版上的VisualStudio2010SP1上看到这个的。如果我将项目属性更改为目标.Net Framework 4,错误就会消失

在Form1.resx文件中,我可以看到System.Drawing assembly的版本明确表示为2.0:

<assembly alias="System.Drawing" name="System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />


有什么想法吗?

这是一个bug。我也看到了。发生这种情况是因为.resx文件指向System.Drawing的4.0.0.0版本,而该版本不存在。为了解决这个问题,我通常在记事本中编辑.resx,将4.0.0.0更改为2.0.0.0。错误是按照您概述的确切步骤引入的。

我找到了一个可能的解决方案,请尝试以下方法:

在设计器中打开resx文件,并将accessmodifier从public设置为no code generation

编辑:有一个解决办法,但很烦人

  • 在Designer中打开表单并进行所需的GUI更改。关闭设计器并保存
  • 编译项目并接收RESX编译错误(只有带有Imagelist的表单才有此问题)
  • 双击resx编译错误以打开resx文件
  • 滚动到imagestream的顶部
  • 编辑图像流的顶行: aaaaad/////aqaaaaaagaaaafdtexn0zw0uv2luzg93cy5gb3jtcywgvmvyc2lvbj00ljaumc4w 到 aaaaaad/////aqaaaaaagaaaafdtexn0zw0uv2luzg93cy5gb3jtcywgvmvyc2lvbj0yljaumc4w
  • 关闭并保存resx文件并重新编译 **注:唯一的区别是“j00LjAuMC4w”结尾的字符与“j0yLjAuMC4w”结尾的字符 每次以设计器模式打开表单时都需要执行此操作

    微软说他们将在下一个VS版本中修复它


    来源:

    如果使用.net 4.5 preview resgen创建资源文件,则可能会出现此问题

    我的笔记本电脑也有同样的问题(Windows7、VS2010高级版、VS11开发者预览版)。 当我在表单上说'localizable=true'时,我在一个简单的表单项目中遇到了这个问题。在我的例子中,没有涉及图像数据。该项目设置为.net 3.5

    private void InitializeComponent()
    {
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
        this.SuspendLayout();
        // 
        // Form1
        // 
        resources.ApplyResources(this, "$this"); //exception Could not load file or assembly 'System.Drawing, Version=4.0.0.0, 
    
    如果我随后将此项目复制到另一台计算机(Windows 7,VS2010 Premium)并尝试调试它,则错误仍然存在。 如果我清理解决方案(而不是项目)(或手动删除bin/obj),错误就会消失 如果我随后将此解决方案复制回我的笔记本电脑,错误将消失,但我无法在“设计”视图中再次看到该表单
    “错误消息:在System.ComponentModel.design.Serialization.CodeDomainDesignerLoader.EnsureDocument(IDesignerSerializationManager管理器)

    这一切的原因似乎是*.Designer.cs文件中的.net版本

  • 运行时版本:4.0.30319.239
  • 运行时版本:4.0.30319.17020在我得到异常的笔记本电脑上

  • 有谁能告诉我,在处理.net 3.5项目时,在哪里可以配置使用哪个resgen版本吗?

    我遇到了这个问题,发现它与.net 4.5 beta有着奇怪的关系。卸载它,然后重新安装.net 4.0会导致问题消失。不确定是否有办法安装.net 4.5 beta并使用.net的资源2.0项目同时进行。

    最近,当客户要求我降级应用程序时,我收到了相同的错误消息


    使用Visual Studio 2010 Professional,我将目标framework从.NET framework 4更改为.NET framework 3.5。然后生成失败,并显示指定的错误消息。解决方案是从应用程序资源中删除导致问题的图像文件。再次添加时,System.Drawing版本被列为2.0.0.0,生成succeeded.

    我遇到了同样的问题,上面的建议对我都不起作用,所以我做了以下几点:

    打开您的项目
    转到解决方案资源管理器
    展开参考组
    删除系统。图纸参考
    右键单击参考组
    添加引用
    在“.NET”选项卡上,搜索System.Drawing以添加正确的参考

    我也有同样的问题


    我修复了.net 4.5.1,它修复了它。

    我也有同样的问题。我的framework 2.0项目继续引用4.0 dll。我修复它的方法…只需转到您的项目参考->选择“系统绘图”->属性,然后选择使用精确版本=true

    这将强制使用2.0框架dll

    希望这有帮助


    干杯!!!

    我也遇到过这个问题,在我的例子中,原因是并行地构建了两个基本上重复的项目,一个目标是.NET 2.0,另一个目标是.NET 4.0,它们都包含几乎相同的代码和资源。如果时机合适,2.0项目将从4.0项目中获取一个输出文件,然后最后引用4.0库

    我通过构建一系列的项目,通过使一个项目依赖于另一个项目来修复它


    (我知道这可能不是@Leonardo发布的原始问题的原因,但对于在类似的双项目设置中遇到此错误的未来访问者可能会有所帮助。)

    我在Xamarin项目中遇到了相同的问题

    实际上,这在Xamarin中不起作用,因为它是windows dll

    看一看讨论
    private void InitializeComponent()
    {
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
        this.SuspendLayout();
        // 
        // Form1
        // 
        resources.ApplyResources(this, "$this"); //exception Could not load file or assembly 'System.Drawing, Version=4.0.0.0, 
    
      /// <summary>
      /// Method that gets called by ManagedResource.WriteData() in project CodeAnalysis during code 
      /// emitting to get the data for an embedded .resx file. Caller guarantees that the returned
      /// MemoryStream object gets disposed.
      /// </summary>
      /// <param name="resourceFullFilename">full path and filename for .resx file to embed</param>
      /// <param name="targetLessThan4">true if necessary to change System.Drawing from 4.0.0.0 to 2.0.0.0</param>
      /// <returns>MemoryStream containing .resources file data for the .resx file</returns>
      [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
      private static MemoryStream ProvideResourceDataForResx(string resourceFullFilename, 
                                                             bool targetLessThan4)
      {
         MemoryStream shortLivedBackingStream = new MemoryStream();
         using (ResourceWriter resourceWriter = new ResourceWriter(shortLivedBackingStream))
         {
            using (ResXResourceReader resourceReader = new ResXResourceReader(resourceFullFilename))
            {
               IDictionaryEnumerator dictionaryEnumerator = resourceReader.GetEnumerator();
               while (dictionaryEnumerator.MoveNext())
               {
                  string resourceKey = dictionaryEnumerator.Key as string;
                  if (resourceKey != null)  // Should not be possible
                     resourceWriter.AddResource(resourceKey, dictionaryEnumerator.Value);
               }
            }
         }
    
         // Get reference to the buffer used by shortLivedBackingStream, which is now closed because
         //  resourceWriter was disposed. If relevant, fix version number for System.Drawing.
         byte[] backingStreamBuffer = shortLivedBackingStream.GetBuffer();
         if (targetLessThan4)
            ChangeSystemDrawingVersionNumber(backingStreamBuffer);
    
         // Create new MemoryStream because shortLivedBackingStream is closed
         return new MemoryStream(backingStreamBuffer);
      }
    
    
      /// <summary>
      /// Method to change the System.Drawing version number from "4.0.0.0" to "2.0.0.0" in the
      /// binary data that represents a .resources file. This implementation is based on the
      /// assumption that character data in the .resources file is in UTF-8 encoding.
      /// </summary>
      private static void ChangeSystemDrawingVersionNumber(byte[] dataBuffer)
      {
         byte[] byteArray1 = Encoding.UTF8.GetBytes("System.Drawing, Version=4.0.0.0");
         byte[] byteArray2 = Encoding.UTF8.GetBytes("System.Drawing, Version=2.0.0.0");
    
         for (int i = 0; i < dataBuffer.Length - byteArray1.Length; i++)
            if (ArrayEquals(byteArray1, dataBuffer, i))
               Array.Copy(byteArray2, 0, dataBuffer, i, byteArray2.Length);
      }
    
    
      /// <summary>
      /// Method to test for a byte array in a larger byte array that is being searched. No error
      /// checking is done - it's assumed an indexing error is not possible.
      /// </summary>
      private static bool ArrayEquals(byte[] searchArray, byte[] searchedArray, 
                                      int searchedArrayIndex)
      {
         for (int i = 0; i < searchArray.Length; i++)
            if (searchArray[i] != searchedArray[searchedArrayIndex + i])
               return false;
         return true;
      }