【发布时间】:2022-01-04 20:12:53
【问题描述】:
我正在使用 CodeDom 编译器和 Microsoft.CSharp,我正在尝试嵌入资源并调用它。我不尝试调用属性的原因是因为我总是收到错误消息Properties does not exist in the current context。所以我想知道是否在做
Parameters.EmbeddedResources.Add("C:/Users/User1/Music/sample.mp3"); 实际上很有帮助,或者我应该以另一种方式来做。这就是我现在在编译器源代码中的内容:
Extract("TestCompiler", "C:/Users/User1/Downloads", "", "Music.mp3");
private static void Extract(string NameSpace, string OutputDir, string InternalPath, string ResourceName){
Assembly assembly = Assembly.GetCallingAssembly();
using (Stream s = assembly.GetManifestResourceStream(NameSpace + "." + (InternalPath == "" ? "" : InternalPath + ".") + ResourceName))
using (BinaryReader r = new BinaryReader(s))
using (FileStream fs = new FileStream(OutputDir + "\\" + ResourceName, FileMode.OpenOrCreate))
using (BinaryWriter w = new BinaryWriter(fs))
w.Write(r.ReadBytes((int)s.Length));
}
当我这样做并运行已编译的 exe 时,这是我得到的异常/错误:
Unhandled Exception: System.ArgumentNullException: Value cannot be null.
Parameter name: input
at System.IO.BinaryReader..ctor(Stream input, Encoding encoding, Boolean leaveOpen)
at TestCompiler.Program.Extract(String NameSpace, String OutputDir, String InternalPath, String ResourceName)
at TestCompiler.Program.Main(String[] args)
我也尝试过Extract("TestCompiler", "C:/Users/User1/Downloads", "Resources", "Music.mp3");,但我得到了同样的错误。
可以调用嵌入式资源还是应该放弃?我已经在这里呆了 3 天了。
【问题讨论】:
标签: c# .net-assembly system.reflection codedom csharpcodeprovider