【问题标题】:csc.exe from C# code来自 C# 代码的 csc.exe
【发布时间】:2017-02-26 18:50:12
【问题描述】:

我想从另一个用 C# 编写的 C# 代码编译。

到目前为止,我已经编写了以下代码:

string cscPath = @"C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe";
string command = "/c \"" + cscPath + "\" /out:\"" + resultsFilePath 
+ "\" /reference:\"" + dllPath 
+ "\" \"" + csFilePath + "\" > \"" + resultsPath + "\\result.txt\"";

Process.Start("cmd.exe", command );

result.txt 文件未创建,我不知道如何检查错误信息。

如果我运行这样的事情:

string command = "/c echo something > \"" + resultsPath + "\\result.txt\"";
Process.Start("cmd.exe", command);

它有效,并且 result.txt 包含“某物”字样。

我使用 Visual Studio 2012。

【问题讨论】:

  • 您是否尝试将command 打印到控制台并在CMD 中手动执行?也许你会发现它甚至不是你之前所期望的命令。
  • 是的。我复制了命令字符串(在调试过程中)并在命令窗口中手动执行。它有效。

标签: c# cmd csc


【解决方案1】:

实际上有可能在运行中编译已经内置的 C# 代码,所以你不需要自己去调用 csc.exe 的麻烦。 看看下面的博客文章,它解释了如何做到这一点: https://blogs.msdn.microsoft.com/abhinaba/2006/02/09/c-writing-extendable-applications-using-on-the-fly-compilation/

如果你想通过调用 csc.exe 来实现,你可以尝试直接调用 csc.exe 而不是通过命令行:

string cscPath = @"C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe";
var cscArguments = "...";
Process.Start(cscPath, cscArguments);

为了找到错误,您可以从命令行运行完全相同的命令并检查那里的输出。这应该会让你走上正轨。

【讨论】:

    猜你喜欢
    • 2021-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多