【发布时间】:2023-03-28 05:35:01
【问题描述】:
我有一个 Windows 窗体应用程序,我用它来生成资源文件。我正在尝试向此应用程序添加此类功能,这将允许我将另一个 Windows 窗体应用程序编译为可执行文件,其中包含这些资源。但是,我坚持编译另一个 Windows 窗体项目部分。
我尝试关注this article,目前我的代码如下所示:
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?)
如果你们能帮助我,我真的很感激,我从来没有从其他项目编译过任何东西。
【问题讨论】:
-
你怎么能只编译一个文件并期望它工作?
-
我认为你应该使用 CompileAssemblyFromFileBatch 传递所有文件,如 Program.cs 和 Form.cs 和 Form.Designer.cs 等等
-
阅读编译器文档而不是“希望”。也就是说,只需致电
msbuild project.csproj即可。
标签: c# compiler-construction csharpcodeprovider