访问使用CodeDom编译的程序中的资源 [英] Access Resources in Program Compiled With CodeDom
问题描述
我正在尝试创建一个程序,它将根据用户指定的参数生成可执行文件,我作为资源添加,但每当我尝试编译程序时,所有访问资源的尝试都会失败。
在生成EXE文件的程序中,我有这个代码来编译EXE:
I am trying to create a program that will generate an executable based on user specified parameters that I add in as resources, but whenever I try to compile the program, all attempts to access the resources fail.
In the program that will generate the EXE file, I have this code to compile the EXE:
Public Sub CompileEXE(ByVal FilePath As String)
Dim ms As New MemoryStream
Me.Properties.Icon.Save(ms)
My.Computer.FileSystem.WriteAllBytes(Application.LocalUserAppDataPath & "\icon.ico", ms.ToArray, False)
Dim cp As CompilerParameters = New CompilerParameters()
cp.ReferencedAssemblies.Add("System.dll")
cp.ReferencedAssemblies.Add("System.Windows.Forms.dll")
cp.ReferencedAssemblies.Add("Microsoft.VisualBasic.dll")
cp.ReferencedAssemblies.Add("System.Drawing.dll")
cp.ReferencedAssemblies.Add("System.Core.dll")
cp.ReferencedAssemblies.Add("mscorlib.dll")
Dim provider As CodeDomProvider = CodeDomProvider.CreateProvider("VisualBasic")
cp.GenerateExecutable = True
cp.OutputAssembly = FilePath
cp.MainClass = "frmThornEXE"
Using res As New ResourceWriter(Application.LocalUserAppDataPath & "\resources.resources")
res.AddResource("icon", Me.Properties.Icon)
res.Generate()
res.Close()
End Using
cp.EmbeddedResources.Add(Application.LocalUserAppDataPath & "\resources.resources")
cp.CompilerOptions = String.Format(" /target:winexe /m:frmThornEXE /win32icon:""{0}""", Application.LocalUserAppDataPath & "\icon.ico")
Dim strCode As String = My.Resources.ThornEXE
strCode = strCode.Replace("{Title}", Me.Properties.Title)
strCode = strCode.Replace("{Version}", Me.Properties.Version.ToString)
strCode = strCode.Replace("{Description}", Me.Properties.Description)
strCode = strCode.Replace("{Author}", Me.Properties.Author)
Dim errors As System.CodeDom.Compiler.CompilerResults = provider.CompileAssemblyFromSource(cp, strCode)
If errors.Errors.HasErrors Then
MessageBox.Show(errors.Errors(0).ToString)
End If
End Sub
这是代码(在生成的程序中)我必须尝试检索资源:
And this is the code(in the generated program) I have to attempt to retrieve the resources:
Dim icostream As System.IO.Stream = Assembly.GetExecutingAssembly.GetManifestResourceStream("icon")
Me.Icon = New Icon(icostream)
但是,这不起作用,icostream变为null。有没有办法访问我添加的资源?(来自生成的EXE)
But, this doesn't work and "icostream" turns up null. Is there any way to access the resources I added?(FROM WITHIN THE GENERATED EXE)
推荐答案
GetExecutingAssembly返回当前正在执行的程序集,即:您的应用程序构建器应用程序,而不是您刚刚从代码中创建了一个.EXE的应用程序。
.NET中没有本地类来编辑单独的.EXE或。的资源。 DLL文件。您必须使用第三方库,或自己动手,才能执行此操作。看一下 ResourceLib [ ^ ]。
GetExecutingAssembly returns the currently executing assembly, i.e.: your application builder app, NOT the app you just created an .EXE for from your code.
There is no native class in .NET to "edit" the resources of a seperate .EXE or .DLL file. You'll have to use a third party library, or roll your own, to do this. Take a look at ResourceLib[^].
这篇关于访问使用CodeDom编译的程序中的资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!