我想使用CSharpCodeProvider
从另一个项目编译项目问题是编译器在当前目录中看到了目标类
CompilerParameters parameters = new CompilerParameters
{
GenerateExecutable = true,
IncludeDebugInformation = true,
GenerateInMemory = false,
TreatWarningsAsErrors = true,
WarningLevel = 3,
CompilerOptions = "/optimize",
OutputAssembly = "Output.exe",
};
CSharpCodeProvider codeProvider = new CSharpCodeProvider();
CompilerResults results = codeProvider.CompileAssemblyFromFile(parameters, new string[] { "../../../targetDirectory/targetClass.cs" });
var warnings = from e in results.Errors.Cast<CompilerError>()
where e.IsWarning
select e;
var errors = from e in results.Errors.Cast<CompilerError>()
where !e.IsWarning
select e;
foreach (var warning in warnings)
{
Console.WriteLine(warning);
}
Console.WriteLine("==========");
foreach (var error in errors)
{
Console.WriteLine(error);
}
输出错误是:错误CS1504:源文件 &#39; c:\ Users \ User \ Documents \ Visual Studio 2017年\项目\ currentDirectory所\ BIN \调试\ targetClass.cs&#39;不可能 打开(&#39;系统无法找到指定的文件。&#39;)
答案 0 :(得分:0)
该文件的路径使用/
作为路径分隔符。在Windows上,路径分隔符为\
。请尝试使用此代码:
CompilerResults results = codeProvider.CompileAssemblyFromFile(parameters, new string[] { @"..\..\..\targetDirectory\targetClass.cs" });