0

I have a project in c# that I would like to compile programmatically and put it in Release folder.

I am using CodeDomProvider, here below is my code:

 class Compiler
    {
      public static void Build(string filename,string outputAssemblyPath)
        {

            CodeDomProvider cdp = CodeDomProvider.CreateProvider("CSharp");
            CompilerParameters cp = new CompilerParameters()
            {
                GenerateExecutable = true,
                GenerateInMemory = true,
                IncludeDebugInformation = true,
                OutputAssembly = outputAssemblyPath
            };
            CompilerResults result = cdp.CompileAssemblyFromFile(cp, filename);

            foreach (string s in result.Output)
            {
                Console.WriteLine(s);
            }
        }
}

I am calling Build method the following way:

 class Program
    {
        static void Main(string[] args)
        {
            Compiler.Build("C:/Projects/Project1/alan.project/Project1.csproj", "C:/Projects/Project1/alan.project/bin/Release/");
            Console.ReadLine();
        }
    }

All I want to do is instead of clicking "Rebuild" and have my project compiled in the Release directory, I want it done programmatically.

8
  • Did you read learn.microsoft.com/dotnet/framework/reflection-and-codedom/…? There seems to be an OutputAssembly-property on the CompilerParameter-argument provided to the CompileFrom...-method. Commented Apr 17, 2018 at 7:01
  • @HimBromBeere I tried the example in that webpage but it doesn't work, I am including the path of "Program.cs" as a first parameter and the location of the exe file as the second param. But there is error CS1504 'path/of/my/compiler/project/bin/release/Program.cs' could not be opened. Why is it reference to my compiler project and not to the project I want to compile? Commented Apr 17, 2018 at 7:12
  • It is principle to build with CodeDomProvider? Commented Apr 17, 2018 at 7:13
  • Did you tried CompilerOptions with /out argument? /out:D:\\Alan\\AssemblyName Commented Apr 17, 2018 at 7:15
  • @UserName Not necessarily with CodeDomProvider, any other one is good, I will try your suggestion Commented Apr 17, 2018 at 7:18

1 Answer 1

2

One of decision it's using MSBuild SDK, that allows create custom task on C#. Through it you can controll build process, describe own logic.


using Microsoft.Build.Construction;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using System.Linq;

namespace MSBuild.Common
{
    class Build : Task
    {
        [Required]
        public string Solution
        {
            get;
            set;
        }

        public override bool Execute()
        {
            return BuildEngine2.BuildProjectFilesInParallel(SolutionFile.Parse(Solution).ProjectsByGuid.Select(p => p.Value.AbsolutePath).ToArray(), null, null, null, null, true, false);
        }
    }
}

This is task example. It's accept path to the solution and perform parallel compile its projects. You can add properties to BuildProjectFilesInParallel that will describe build process.


Call of created task will be from the custom target:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" InitialTargets="EntryBuild">

    <UsingTask AssemblyFile="Build.dll" TaskName="Build" />

    <Target Name="EntryBuild">
        <Build Solution="$(Solution)" />
    </Target>

</Project>

Execute it through MSBuild CLI:

msbuild.exe /t:Build.targets /p:Solutin=%pathToSolution%

MSBuild Targets | Task Writing

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.