【发布时间】:2012-10-13 11:07:56
【问题描述】:
我正在尝试使用 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();
}
}
}
}
如何提取作为资源嵌入的 DLL 文件并将其保存到 System32?
【问题讨论】:
-
Rafik,你应该在下面查看 Thomas Sapp 的答案,因为它绝对比你接受的要好。
标签: c# .net embedded-resource csharpcodeprovider