2

I am new to C# and am trying to use .NET's CodeDom.Compiler to compile an application and properly generate the assembly information in the outputted exe. I've been looking in the MS Docs / Class reference to do so. Is it possible to set the assembly information during compilation?

Code to compile my source code:

CompilerParameters CParams = new CompilerParameters();
CParams.GenerateExecutable = true;
CParams.OutputAssembly = Output;

string options = "/optimize+ /platform:x86 /target:winexe /unsafe";
if (Icon != null)
    options += " /win32icon:\"" + Icon + "\"";

CParams.CompilerOptions = options;
CParams.TreatWarningsAsErrors = false;
CParams.ReferencedAssemblies.Add("System.dll");
CParams.ReferencedAssemblies.Add("System.Windows.Forms.dll");
CParams.ReferencedAssemblies.Add("System.Drawing.dll");
CParams.ReferencedAssemblies.Add("System.Data.dll");
CParams.ReferencedAssemblies.Add("Microsoft.VisualBasic.dll");

Dictionary<string, string> ProviderOptions = new Dictionary<string, string>();
ProviderOptions.Add("CompilerVersion", "v2.0");

CompilerResults Results = new CSharpCodeProvider(ProviderOptions).CompileAssemblyFromSource(CParams, source);

Outputted Exe's Assembly Information:

enter image description here

Is this function available when I'm actually saving the exe using the Writer?

Writer.WriteResource(FSave.FileName, EncryptedBytes);

I greatly appreciate any leads, thank you.

Nathan_Sharktek
  • 407
  • 1
  • 5
  • 21
  • also duplicate https://stackoverflow.com/questions/2989246/how-can-i-change-assembly-version-assembly-file-version-by-compliling-code-using – m4a Sep 27 '18 at 19:43
  • Possible duplicate of [How can I change Assembly Version,Assembly File Version by compliling code using System.CodeDom.CodeCompileUnit](https://stackoverflow.com/questions/2989246/how-can-i-change-assembly-version-assembly-file-version-by-compliling-code-using) – Richardissimo Sep 27 '18 at 20:05

1 Answers1

1

Instead your last line

        ...
        var unit = new CodeCompileUnit();
        var attr = new CodeTypeReference(typeof(AssemblyVersionAttribute));
        var decl = new CodeAttributeDeclaration(attr, new CodeAttributeArgument(new CodePrimitiveExpression("1.0.2.42")));
        unit.AssemblyCustomAttributes.Add(decl);
        var prov = new CSharpCodeProvider(ProviderOptions);
        var assemblyInfo = new StringWriter();
        prov.GenerateCodeFromCompileUnit(unit, assemblyInfo, new CodeGeneratorOptions());

        var result = prov.CompileAssemblyFromSource(CParams, new[] {"public class p{public static void Main(){}}", assemblyInfo.ToString()});

generate assemblyinfo and add this to source for compilation

msdn

m4a
  • 187
  • 1
  • 11
  • 1
    Thank you for this code snippet, which might provide some limited, immediate help. A [proper explanation](https://meta.stackexchange.com/q/114762/349538) would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you’ve made. – Richardissimo Sep 27 '18 at 20:04