如何从嵌入的资源中提取文件并将其保存到磁盘? [英] How can i extract a file from an embedded resource and save it to Disk?
问题描述
我试图编译以下使用CSHARP codeProvider的code,该文件被成功编译,但是当我点击生成exe文件,我得到一个错误(Windows正在寻找一个解决这个问题),并没有任何反应。
I'm trying to compile the code below using CSharpCodeProvider, the file is successfully compiled but when i click on the generated exe, i get an error (Windows is searching for a solution to this problem) and nothing happens.
当我编译下面使用CSHARP codeProvider的code,我已经添加了 MySql.Data.dll
作为嵌入资源文件中使用这一行的code:
When i compile the code below using CSharpCodeProvider, i've added the MySql.Data.dll
as an embedded resource file using this line of code :
if (provider.Supports(GeneratorSupport.Resources))
cp.EmbeddedResources.Add("MySql.Data.dll");
该文件被成功嵌入(因为我注意到,文件大小增加)
The file is successfully embedded ( because i noticed the file size increased)
在code以下,我尝试做的是提取嵌入式dll文件并将其保存到System32下 但低于code没有出于某种原因。
In the code below, what i try to do is to extract the embedded dll file and save it to System32 but the code below doesn't work for some reason.
namespace ConsoleApplication1
{
class Program
{
public static void ExtractSaveResource(String filename, String location)
{
// Assembly assembly = Assembly.GetExecutingAssembly();
Assembly a = .Assembly.GetExecutingAssembly();
// Stream stream = assembly.GetManifestResourceStream("Installer.Properties.mydll.dll"); // or whatever
// string my_namespace = a.GetName().Name.ToString();
Stream resFilestream = a.GetManifestResourceStream(filename);
if (resFilestream != null)
{
BinaryReader br = new BinaryReader(resFilestream);
FileStream fs = new FileStream(location, FileMode.Create); // say
BinaryWriter bw = new BinaryWriter(fs);
byte[] ba = new byte[resFilestream.Length];
resFilestream.Read(ba, 0, ba.Length);
bw.Write(ba);
br.Close();
bw.Close();
resFilestream.Close();
}
// this.Close();
}
static void Main(string[] args)
{
try
{
string systemDir = Environment.SystemDirectory;
ExtractSaveResource("MySql.Data.dll", systemDir);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadKey();
}
}
}
}
我怎么能提取嵌作为资源DLL文件并将其保存到System32下?
How can i extract the DLL file that is embedded as a resource and save it to System32?
推荐答案
我一直在使用这种(测试)方法:
I've been using this (tested) method:
OutputDir:要复制的资源位置
OutputDir: Location where you want to copy the resource
ResourceLocation:命名空间(+ dirnames中)
ResourceLocation: Namespace (+ dirnames)
文件:在resourcelocation内的文件列表,要复制
Files: List of files within the resourcelocation, you want to copy.
private static void ExtractEmbeddedResource(string outputDir, string resourceLocation, List<string> files)
{
foreach (string file in files)
{
using (System.IO.Stream stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceLocation + @"." + file))
{
using (System.IO.FileStream fileStream = new System.IO.FileStream(System.IO.Path.Combine(outputDir, file), System.IO.FileMode.Create))
{
for (int i = 0; i < stream.Length; i++)
{
fileStream.WriteByte((byte)stream.ReadByte());
}
fileStream.Close();
}
}
}
}
这篇关于如何从嵌入的资源中提取文件并将其保存到磁盘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!