我想使用CSharpCodeProvider编译下面的代码。该文件已成功编译,但是当我单击生成的EXE文件时,出现错误(Windows正在寻找解决此问题的解决方案),但没有任何反应。如何从嵌入式资源提取文件并将其保存到磁盘?
当我编译下面使用CSharpCodeProvider的代码,我已经添加了MySql.Data.dll
为使用该行代码嵌入的资源文件:
if (provider.Supports(GeneratorSupport.Resources))
cp.EmbeddedResources.Add("MySql.Data.dll");
该文件被成功嵌入(因为我注意到,文件大小增加)。
在下面的代码中,我尝试提取嵌入的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();
}
}
}
}
如何提取嵌入为资源并将其保存到System32的DLL文件?
哈里,你应该在下面回顾Thomas Sapp的答案,因为它比你接受的答案要好得多。 – motoDrizzt