【问题标题】:Extract file from Ressources从资源中提取文件
【发布时间】:2015-08-09 16:07:40
【问题描述】:

我正在尝试从项目中的资源中提取 .zip 文件,因此在网上找到了这个使用 SharpZipLib 的出色示例:

try
{
   ResourceManager objResMgr = new ResourceManager("My_Project.MyFile", Assembly.GetExecutingAssembly());
   byte[] objData = (byte[])objResMgr.GetObject("MyFile.zip");
   MemoryStream objMS = new MemoryStream(objData);
   ZipInputStream objZIP = new ZipInputStream(objMS);
   ZipEntry theEntry;
   while ((theEntry = objZIP.GetNextEntry()) != null)
   {
       FileStream streamWriter =
       File.Create(System.IO.Path.Combine("C:/", theEntry.Name));
       int size = objData.Length;
       byte[] data = new byte[size];
       while (true)
       {
           size = objZIP.Read(data, 0, data.Length);
           if (size > 0)
           {
              streamWriter.Write(data, 0, size);
           }
           else
           {
              break;
           }
      }
      streamWriter.Close();
   }
   objZIP.Close();
}
catch (Exception ex)
{
   MessageBox.Show(ex.Message);
}

问题是,当我运行我的代码时,我收到一个异常,要求我检查“My_Project.MyFile.ressources”是否已正确嵌入或链接到程序集“我的项目”中。 如果我的 zip 的 Build Action 是“Ressource”或“Embedded Ressource”,就会出现这种情况。我确信 zip 文件最终会出现在 .exe 中,因为它的大小。

【问题讨论】:

    标签: c# wpf stream zip


    【解决方案1】:

    我怀疑问题在于您用于ResourceManager 构造函数的第一个参数的字符串。

    您使用“My_Project.MyFile”。为此,您必须在 My_Project 项目中拥有一个名为“MyFile.resx”的 .resx 资源文件(它不是作为资源嵌入的文件的名称)。 resx 文件就像资源管理器使用的资源目录。

    要使用您拥有的代码,请在项目中添加一个新的资源文件 (.resx),在 VS 中打开它,将该文件添加为新资源,然后使用 ResourceManager 即可使用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-17
      • 1970-01-01
      • 2013-11-20
      • 2016-05-30
      • 1970-01-01
      相关资源
      最近更新 更多