Hi, I want know if is possible to use a ressource added in my codedom compiled programme.
Exemple of my CodeDom compiler:
Code Public Shared Sub COMPILE(ByVal OUTPUT As String, ByVal CODE As String, ByVal ICON As String, RESSOURCE As String)
On Error Resume Next
Dim Compiler As ICodeCompiler = (New VBCodeProvider).CreateCompiler()
Dim Parameters As New CompilerParameters()
Dim cResults As CompilerResults
Parameters.GenerateExecutable = True
Parameters.OutputAssembly = OUTPUT
Parameters.ReferencedAssemblies.Add("System.dll")
Parameters.ReferencedAssemblies.Add("System.Data.dll")
Parameters.ReferencedAssemblies.Add("System.Windows.Forms.dll")
If Not RESSOURCE = Nothing Then
Parameters.EmbeddedResources.Add(RESSOURCE) ' Add a ressource is ok
End If
In My CodeDom Code:
Code My.Computer.FileSystem.WriteAllBytes("C:\user...", My.Resources.File, False) ' Not Working in CodeDom :( help please :)
My But is; inject a ressource in my codedom app ( it's ok ) and Extract this ressource with a codedom app.
Humm no other solutions ? More simple !??
(May 19, 2013 09:08 AM)MemoryCarter Wrote: Humm no other solutions ? More simple !??
It's really simple , You get the current assembly loaded and read the .resources file from it. You get the object (normally string) 's "title" then get the data related to it.
There is something I wrote really quick , maybe it will give you ideas :
Quote: 'write resources
Dim r As New System.Resources.ResourceWriter("path/resourcefilename.resources")
r.AddResource("my title", "my data as string/byte or whatever")
r.Generate()
r.Close()
'Read it back
Dim mydata As String = New System.Resources.ResourceManager("resourcefilename", System.Reflection.Assembly.GetExecutingAssembly).GetObject("my title")
(May 19, 2013 09:08 AM)MemoryCarter Wrote: Humm no other solutions ? More simple !??
Code Imports System.CodeDom.Compiler
Imports System.IO
Imports System.Resources
Public Shared Sub COMPILE(ByVal OUTPUT As String, ByVal CODE As String, ByVal ICON As String, ByVal RESNAME As String, ByVal RESBYTES As Byte())
Dim ResourceWriter As New ResourceWriter("res.resources")
ResourceWriter.AddResource(RESNAME, RESBYTES)
ResourceWriter.Close()
Dim Compiler As ICodeCompiler = (New VBCodeProvider).CreateCompiler()
Dim Params As New CompilerParameters()
Dim Results As CompilerResults
Dim Errors As String = Nothing
Params.GenerateExecutable = True
Params.ReferencedAssemblies.Add("System.dll")
Params.ReferencedAssemblies.Add("System.Drawing.dll")
Params.ReferencedAssemblies.Add("System.Data.dll")
Params.ReferencedAssemblies.Add("System.Windows.Forms.dll")
Params.CompilerOptions = " /platform:x86"
Params.OutputAssembly = OUTPUT
If IO.File.Exists(ICON) Then Params.CompilerOptions &= " /win32icon:" & Chr(34) & ICON & Chr(34)
Dim Version = New Dictionary(Of String, String)
Version.Add("CompilerVersion", "v2.0")
Params.CompilerOptions &= " /optimize"
Params.CompilerOptions &= " /t:winexe"
Params.EmbeddedResources.Add("res.resources")
Results = Compiler.CompileAssemblyFromSource(Params, CODE)
If Results.Errors.Count > 0 Then
For Each CompilerError In Results.Errors
Errors &= "Error: " & CompilerError.ErrorText & vbNewLine
Next
MessageBox.Show(Errors, "Error")
Else
MessageBox.Show("All done")
End If
End Sub
(May 19, 2013 09:08 AM)MemoryCarter Wrote: Humm no other solutions ? More simple !??
Dim BBYYTTEE As Byte() = IO.File.ReadAllBytes(My.Resources.YourFile) '< here the bytes read into memory!
System.IO.File.WriteAllBytes(Directory.GetCurrentDirectory & "yournewfilename.exe", BBYYTTEE) '<and here the bytes are written to the specified path name!
Oh, before I forget:
Set your My.Resources.YourFile to : add as Embedded Resources from the properties window
and set to Copy Alway's
Code Dim BBYYTTEE As Byte() = IO.File.ReadAllBytes(My.Resources.name)
System.IO.File.WriteAllBytes("C:\Users\Admin\Desktop\" & "yournewfilename.exe", BBYYTTEE)
Not compatible in CodeDom.
(May 19, 2013 09:22 AM)Felix.Frogg Wrote: Code Imports System.CodeDom.Compiler
Imports System.IO
Imports System.Resources
Public Shared Sub COMPILE(ByVal OUTPUT As String, ByVal CODE As String, ByVal ICON As String, ByVal RESNAME As String, ByVal RESBYTES As Byte())
Dim ResourceWriter As New ResourceWriter("res.resources")
ResourceWriter.AddResource(RESNAME, RESBYTES)
ResourceWriter.Close()
Dim Compiler As ICodeCompiler = (New VBCodeProvider).CreateCompiler()
Dim Params As New CompilerParameters()
Dim Results As CompilerResults
Dim Errors As String = Nothing
Params.GenerateExecutable = True
Params.ReferencedAssemblies.Add("System.dll")
Params.ReferencedAssemblies.Add("System.Drawing.dll")
Params.ReferencedAssemblies.Add("System.Data.dll")
Params.ReferencedAssemblies.Add("System.Windows.Forms.dll")
Params.CompilerOptions = " /platform:x86"
Params.OutputAssembly = OUTPUT
If IO.File.Exists(ICON) Then Params.CompilerOptions &= " /win32icon:" & Chr(34) & ICON & Chr(34)
Dim Version = New Dictionary(Of String, String)
Version.Add("CompilerVersion", "v2.0")
Params.CompilerOptions &= " /optimize"
Params.CompilerOptions &= " /t:winexe"
Params.EmbeddedResources.Add("res.resources")
Results = Compiler.CompileAssemblyFromSource(Params, CODE)
If Results.Errors.Count > 0 Then
For Each CompilerError In Results.Errors
Errors &= "Error: " & CompilerError.ErrorText & vbNewLine
Next
MessageBox.Show(Errors, "Error")
Else
MessageBox.Show("All done")
End If
End Sub
Good, but how to use the .resources in my codedom code ? extract and what is the ".resources" ?? extension
What does your file do?
You select a file and then press compile and the res.resource should be added or bind (from the same folder) with the file you selected?
Rephrase:
What is your program supposed to do?
(May 19, 2013 09:45 AM)MemoryCarter Wrote: Good, but how to use the .resources in my codedom code ? extract and what is the ".resources" ?? extension
Code Dim ResourceManager As New ResourceManager(RESNAME, GetExecutingAssembly())
Dim EmbeddedBytes As Byte() = ResourceManager.GetObject("your_reesource_name")
and than you can do anything with "EmbeddedBytes" bytes array - drop, execute, modify etc.
(May 19, 2013 09:51 AM)InVista Wrote: What does your file do?
You select a file and then press compile and the res.resource should be added or bind (from the same folder) with the file you selected?
Rephrase:
What is your program supposed to do?
I use a .ressources extension but my ressource is a .exe WTF ?
(May 19, 2013 09:54 AM)MemoryCarter Wrote: I use a .ressources extension but my ressource is a .exe WTF ?
What are you trying to do?
The resource file to add (in my example) as embedded is res.resources. You can't append new embedded resource in codedom by code in 6th post
If you want add multi-resource (as embedded), you should use code like that:
Code Dim ResourceWriter As New ResourceWriter("res.resources")
ResourceWriter.AddResource("file1", IO.File.ReadAllBytes("c:\file1.exe"))
ResourceWriter.AddResource("file2", IO.File.ReadAllBytes("c:\file2.exe"))
ResourceWriter.AddResource("file3", IO.File.ReadAllBytes("c:\file3.exe"))
ResourceWriter.Close()
You select a file, which should be added as the new "res.resources" file to another file?
I don't know the rest of your code and therefore I don't know what your program is supposed to do.
Here is what I know:
I see a button and a textbox and another button.
My but is:
Step1: Add a .exe in my CodeDom programme and compile. ( is ok )
Step2: On CodeDom programm run, then extract the .exe of the CodeDom programme
But i Learn the codedom, is difficult :/ wtf!!
( sorry for my bad langage ..)
I think you double compile : you have the SUB COMPILE and you , in your post, called your My.Resources.Code, which is called in the SUB COMPILE.
You build an .exe with a file inside which you added via the codeom.
When the file is clicked the file runs and extracts, also, the in step1 added ,as resources added, file?
Edit:
Textbox1 = the file to add?
(May 19, 2013 10:10 AM)InVista Wrote: I think you double compile : you have the SUB COMPILE and you , in your post, called your My.Resources.Code, which is called in the SUB COMPILE.
You build an .exe with a file inside which you added via the codeom.
When the file is clicked the file runs and extracts, also, the in step1 added ,as resources added, file?
Edit:
Textbox1 = the file to add?
I do not understand, have you teamviewer ?
(May 19, 2013 10:10 AM)InVista Wrote: I think you double compile : you have the SUB COMPILE and you , in your post, called your My.Resources.Code, which is called in the SUB COMPILE.
You build an .exe with a file inside which you added via the codeom.
When the file is clicked the file runs and extracts, also, the in step1 added ,as resources added, file?
Edit:
Textbox1 = the file to add?
Yes textbox1 is a file to add in a ressources !!
|