1

我想创建一个这样的脚本:

static int Main(string[] args)  
{
    if ( !File.Exists( @"E:\Ivara\Cache\TimeCacheLastUpdated.txt" ) )
    {
        return -1;
    }
    return 6;
}

并像这样运行它并获得错误级别:

"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\Roslyn\csi.exe" E:\build\CruiseControl\bin\TestLoggingConfigExists.csx

回声 %ERRORLEVEL%

4

2 回答 2

2

编辑2:这次简单多了

在脚本末尾将退出代码传递给 Environment.Exit 即可完成这项工作。

static int Main(string[] args)
{
    if ( !File.Exists( @"E:\Ivara\Cache\TimeCacheLastUpdated.txt" ) )
    {
        return -1;
    }
    return 6;
}

// csi.exe normally ignores the exit code from Main, but we can terminate
// the csi.exe process abruptly, and forward the exit code to the caller.
Environment.Exit(Main(Args.ToArray()));
于 2019-02-07T18:19:20.653 回答
2

您正在运行的程序csi.exe%ERRORLEVEL%反映该程序的退出代码(即在成功编译的情况下为零,否则为非零),而不是它运行的脚本。

想到的几个选项:

  1. 而是使用csc.exe从您的脚本生成可执行应用程序。运行它并捕获它的退出代码。

  2. 如果由于某种原因需要一次性完成所有这些,请创建一个小应用程序,将脚本路径作为输入参数,csc.exe生成可执行文件,运行它,并编组其退出代码。(或使用内存编译+执行替代方案:https ://josephwoodward.co.uk/2016/12/in-memory-c-sharp-compilation-using-roslyn )

于 2018-01-19T22:38:16.537 回答