[英]Add Embedded Resource to specific path from Assembly compiled at Runtime
[英]Reference Embedded Resource from Codedom Compiled Exe
我正在使用 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天。
要回答我自己的問題,我必須通過以下方式獲取所有資源:
string[] Resources = Assembly.GetExecutingAssembly().GetManifestResourceNames();
為了引用和提取它們,我這樣做了:
foreach(string Resource in Resources)
WriteResources(Resources[Resource], "C:\\Test\\example.mp3");
public static void WriteResources(string Name, string Output){
using(var Resource = Assembly.GetExecutingAssembly().GetManifestResourceStream(Name))
using(var File = new FileStream(Output, FileMode.Create, FileAccess.Write))
Resource.CopyTo(File);
}
幸運的是,經過幾天的努力,我能夠完成我的項目。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.