Click here to Skip to main content
15,881,581 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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:
VB
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:
VB
Dim icostream As System.IO.Stream = Assembly.GetExecutingAssembly.GetManifestResourceStream("icon")
Me.Icon = New Icon(icostream)

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)
Posted
Updated 30-Nov-13 11:32am
v5
Comments
Ron Beyer 26-Nov-13 10:30am    
Why are you storing them in an EXE? Why not a RESX (compiled resources file) or a DLL?
ianmartinez97 28-Nov-13 0:11am    
The EXE I generated is supposed to run on its own and I'm not trying to access them from within the program generating the EXE, I'm trying to access them from the generated EXE itself.

1 solution

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[^].
 
Share this answer
 
v2
Comments
ianmartinez97 29-Nov-13 4:31am    
The GetExecutingAssembly is inside the EXE I created and I'm trying to access the resources from the generated EXE itself.
Dave Kreskowiak 29-Nov-13 10:40am    
Thanks for the 1-vote. I already told you where to look and why what you're doing will not work.
ianmartinez97 29-Nov-13 22:38pm    
Well, you told me "There is no native class in .NET to "edit" the resources of a seperate .EXE or .DLL file." That's not what I'm trying to do. I'm not trying to edit or read the resources of a separate .EXE or .DLL file.

The problem I am having is not being able to have the generated .EXE access ITS OWN resources when it runs by itself without any help from the application builder app. So if GetExecutingAssembly won't work inside of the generated .EXE, what way will work to access the "Icon" resource from within the generated .EXE?
Dave Kreskowiak 29-Nov-13 23:57pm    
Thanks for clarifying. In that case, I have no idea. I just don't have the time to build an entire test project to find out.
ianmartinez97 30-Nov-13 1:44am    
Thanks anyways

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900