Merge ResX file at runtime?

I generate / create from the code a custom resource file with .Net control ( .resx file)

In C # or VB.Net , how could I develop a method capable of combining, combining, or hiring my .resx file into a .net assembly (bigger or smaller just like the default VS compiler)?

So, in one hand I have Application.exe (already compiled), and on the other hand I have resources.resx , I intend to "combine" resx into a compiled assembly.

Any information would be appreciated because I did not find anything about it.

PS: The only requirement is not to use third-party tools like ILMerge .

+5
source share
2 answers

Finally, I decided on a slightly different approach, after I created the ResX file, what I do is compile the .net assembly at runtime and assign this ResX file as an inline assembly resource that needs to be compiled, so the result is a compiled dll dll that can be merged or whatever, and I can extract the ResX file at any time from this dll, nice !.

If someone knows a simpler / simpler alternative, feel free to post the answer (for reference or just for the remaining reward)

Here is a sample code:

Imports System.CodeDom.Compiler Imports System.IO Imports System.Reflection Public Class Form1 Private Sub Test() Handles MyBase.Shown ' Create the ResX file. Dim resX As New ResXManager(Path.Combine(Application.StartupPath, "C:\MyResources.resx")) With resX .Create(replace:=True) .AddResource(Of String)("String Resource", "Hello World!", "String Comment") End With ' Compile an assembly.dll that contains the ResX file as an embedded resource. Dim codeProvider As CodeDomProvider = CodeDomProvider.CreateProvider("VB") ' or New VBCodeProvider() Dim parameters As CompilerParameters = New CompilerParameters() With parameters .GenerateExecutable = False .OutputAssembly = "C:\Assembly.dll" .EmbeddedResources.Add("C:\MyResources.resx") End With Dim results As CompilerResults = codeProvider.CompileAssemblyFromSource(parameters, "Public class ResourceClass : End Class") ' Read the assembly. Dim assembly As Assembly = assembly.LoadFile("c:\Assembly.dll") ' Extract/read the ResX file from assembly. Dim embeddedFileName As String = "MyResources.resx" Dim targetFilePath As String = "C:\NewMyResources.resx" Using s As Stream = assembly.GetManifestResourceStream(embeddedFileName) Dim buffer As Byte() = New Byte(CInt(s.Length - 1)) {} Dim read As Integer = s.Read(buffer, 0, CInt(s.Length)) Using fs As New FileStream(targetFilePath, FileMode.Create) fs.Write(buffer, 0, buffer.Length) End Using End Using End Sub End Class 

I did some help to do this from here:

Dynamically creating a DLL assembly at runtime

Read the embedded file from the assembly

+3
source

You cannot combine resources into an already compiled file without external tools (manual or existing). Perhaps that is why you did not find any information about this. it is possible to merge the file manually, but I would recommend a different approach.

If possible, you can try to generate a resource file before compilation, for example, using a text template in Visual Studio. Then, during compilation, the usual behavior will be applied to the generated resx file, which will be removed in the assembly.

If generating a resource file before compilation is not possible, you will have to use an external tool

+6
source

Source: https://habr.com/ru/post/1215411/


All Articles