无法编译来自不同项目 C# 的文件

Cannot compile file from different project C#

我想使用 CSharpCodeProvider 从另一个项目编译一个项目

问题是编译器在当前目录

中看到了目标class
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);
}

The output Error is: error CS1504: Source file 'c:\Users\User\Documents\Visual Studio 2017\Projects\CurrentDirectory\bin\Debug\targetClass.cs' could not be opened ('The system cannot find the file specified. ')

文件路径使用 / 作为路径分隔符。在 Windows 上,路径分隔符是 \。试试改用这个:

CompilerResults results = codeProvider.CompileAssemblyFromFile(parameters, new string[] { @"..\..\..\targetDirectory\targetClass.cs" });