簡體   English   中英

從另一個項目編譯整個項目

[英]Compile entire project from another project

我有一個Windows Forms應用程序,用於生成資源文件。 我正在嘗試向此應用程序添加此類功能,這將允許我將另一個Windows Forms應用程序編譯為可執行文件,其中將包含這些資源。 但是,我堅持要編譯另一個Windows Forms項目部分。

我試圖遵循本文 ,到目前為止我的代碼如下:

CompilerParameters parameters = new CompilerParameters();
parameters.GenerateExecutable = true;
parameters.IncludeDebugInformation = true;
parameters.GenerateInMemory = false;
//parameters.TreatWarningsAsErrors = true;
parameters.WarningLevel = 3;
parameters.CompilerOptions = "/optimize";
parameters.OutputAssembly = "Program.exe";

CSharpCodeProvider codeProvider = new CSharpCodeProvider();
CompilerResults results = codeProvider.CompileAssemblyFromFile(parameters, new string[] { "Program.cs" });

我不確定是否正確執行了此操作(如果應編譯Program.cs文件)。 Program.cs文件如下所示:

using System;
using System.Windows.Forms;

namespace example
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

當我嘗試使用上面的代碼進行編譯時,出現此錯誤:

Line number 16, Error Number: CS0246, 'The type or namespace name 'Form1' could not be found (are you missing a using directive or an assembly reference?)

如果你們能幫助我,我將不勝感激,我從未編譯過其他項目的任何內容。

假設您要編譯的另一個項目是project.csproj,請引用Microsoft.Build.Framework並使用以下代碼:

var globalProperty = new Dictionary<string, string> { { "Configuration", "Debug"}, { "Platform", "Any CPU" } };
var buildParameters = new BuildParameters(new ProjectCollection()) { Loggers = new List<ILogger> { new ConsoleLogger() } };
var buildRequest = new BuildRequestData(@"C:\example\project.csproj", globalProperty, "4.0", new[] {"Build" }, null);
BuildResult buildResult = BuildManager.DefaultBuildManager.Build(buildParameters, buildRequest);

或者,您可以只包裝MsBuild.exe

var p = new Process(); 
p.StartInfo = new ProcessStartInfo(@"C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe");
p.StartInfo.Arguments = @"C:\example\project.csproj";
p.Start();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM