如何从资源中保存二进制文件? [英] How to save a binary file from resources?

查看:277
本文介绍了如何从资源中保存二进制文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果这行是正确的,接下来要写什么?:

If this line is correct, what to write next?:

Stream s = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("some_file");


其他
怎么样?
找到了,但是不能使用:我在资源中有0.exe命名为"_0",我尝试过:


else
How?
Found this, but can''t use: I have 0.exe named as "_0" in resources, I tried:

ExtractEmbeddedBinaryFile(Assembly.GetExecutingAssembly(), "_0", "dest.exe");
ExtractEmbeddedBinaryFile(Assembly.GetExecutingAssembly(), "0", "dest.exe");
ExtractEmbeddedBinaryFile(Assembly.GetExecutingAssembly(), "0.exe", "dest.exe");


-没有成功-找不到文件异常.


- without success - File not found exception.

public static void ExtractEmbeddedBinaryFile(Assembly assembly, string resourceName, string targetFile)
 {
     FileInfo assemblyFileInfo = new FileInfo(assembly.Location);

     // delete local copy of existing binary file if it is older than assembly
     if (File.Exists(targetFile))
     {
         if (new FileInfo(targetFile).CreationTime < assemblyFileInfo.LastWriteTime)
             File.Delete(targetFile);
     }

     // extract local copy of binary file from assembly
     if (!File.Exists(targetFile))
     {
         Stream streamIn = assembly.GetManifestResourceStream(resourceName);
         if (streamIn == null)
         {
             throw new Exception("Embedded resource ''" + resourceName + "'' not found in ''" + assembly.Location + "''.");
         }
         Stream streamOut = File.Create(targetFile);
         BinaryReader br = new BinaryReader(streamIn);
         BinaryWriter bw = new BinaryWriter(streamOut);
         if (streamIn.Length > int.MaxValue)
         {
             throw new Exception("Embedded resource ''" + resourceName + "'' in ''" + assembly.Location + "'' is too large.");
         }
         bw.Write(br.ReadBytes((int)streamIn.Length));
         bw.Flush();
         bw.Close();
         br.Close();

         // set the creation time of the local binary file to the last modification time of the assembly for future comparison
         new FileInfo(targetFile).CreationTime = assemblyFileInfo.LastWriteTime;
     }
 }

推荐答案

假定您的Stream将使用该语句正确初始化. (我太忙了,无法实际尝试)

然后查看有关如何将其保存到文件的信息.
Assuming that your Stream will be properly initialized using that statement. (I''m too busy to actually try it)

Then check out this on how to save it to a file.


这篇关于如何从资源中保存二进制文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆