如何从embedded式资源中提取文件并将其保存到磁盘?
我想使用CSharpCodeProvider编译下面的代码。 该文件被成功编译,但是当我点击生成的EXE文件,我得到一个错误(Windows正在寻找解决这个问题),没有任何反应。
当我使用CSharpCodeProvider编译下面的代码时,我已经使用这行代码将MySql.Data.dll
添加为embedded的资源文件:
if (provider.Supports(GeneratorSupport.Resources)) cp.EmbeddedResources.Add("MySql.Data.dll");
该文件成功embedded(因为我注意到文件大小增加)。
在下面的代码中,我尝试提取embedded的DLL文件并将其保存到System32,但下面的代码由于某种原因不起作用。
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(); } } } }
我怎样才能提取作为资源embedded的DLL文件,并将其保存到System32?
我一直在使用这个(testing)的方法:
OutputDir:您想要复制资源的位置
ResourceLocation:命名空间(+ dirnames)
文件:您要复制的资源位置内的文件列表。
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(); } } } }
我build议这样做更容易。 我假设资源存在,文件是可写的(如果我们正在谈论系统目录,这可能是一个问题)。
public void WriteResourceToFile(string resourceName, string fileName) { using(var resource = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)) { using(var file = new FileStream(fileName, FileMode.Create, FileAccess.Write)) { resource.CopyTo(file); } } }
我发现最简单的方法是使用Properties.Resources
和File
。 这是我使用的代码…
对于二进制文件: File.WriteAllBytes(fileName, Properties.Resources.file);
对于文本文件: File.WriteAllText(fileName, Properties.Resources.file);
这完美的作品!
public static void Extract(string nameSpace, string outDirectory, string internalFilePath, string resourceName) { //nameSpace = the namespace of your project, located right above your class' name; //outDirectory = where the file will be extracted to; //internalFilePath = the name of the folder inside visual studio which the files are in; //resourceName = the name of the file; Assembly assembly = Assembly.GetCallingAssembly(); using (Stream s = assembly.GetManifestResourceStream(nameSpace + "." + (internalFilePath == "" ? "" : internalFilePath + ".") + resourceName)) using (BinaryReader r = new BinaryReader(s)) using (FileStream fs = new FileStream(outDirectory + "\\" + resourcename, FileMode.OpenOrCreate)) using (BinaryWriter w = new BinaryWriter(fs)) { w.Write(r.ReadBytes((int)s.Length)); } }
使用示例:
public static void ExtractFile() { String local = Environment.CurrentDirectory; //gets current path to extract the files Extract("Geral", local, "Arquivos", "bloquear_vbs.vbs"); }
如果这仍然没有帮助,请尝试下列video: https : //www.youtube.com/watch?v = _61pLVH2qPk
尝试将目标程序集读入一个MemoryStream
,然后像这样保存到FileStream
(请注意,此代码未经testing):
Assembly assembly = Assembly.GetExecutingAssembly(); using (var target = assembly.GetManifestResourceStream("MySql.Data.dll")) { var size = target.CanSeek ? Convert.ToInt32(target.Length) : 0; // read your target assembly into the MemoryStream MemoryStream output = null; using (output = new MemoryStream(size)) { int len; byte[] buffer = new byte[2048]; do { len = target.Read(buffer, 0, buffer.Length); output.Write(buffer, 0, len); } while (len != 0); } // now save your MemoryStream to a flat file using (var fs = File.OpenWrite(@"c:\Windows\System32\MySql.Data.dll")) { output.WriteTo(fs); fs.Flush(); fs.Close() } }