从批处理脚本运行.Net Core应用程序并重定向异常

Ber*_*ian 0 c# batch-file io-redirection .net-core

NET Core Console App`,并将异常重定向到文件。我还为此脚本提供参数:

到目前为止我的脚本:

rem **calling C# solution from batch*******
@echo received from python param1:%1 , param2:%2 ,param3:%3
start "Dotnet Test" D:\adi\NET\Bench\Bench\bin\Debug\netcoreapp2.1\Bench.dll  dotnet %1 %2 %3 2>output.txt
pause
Run Code Online (Sandbox Code Playgroud)

.NET Core 控制台应用程序

class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(args[0]);
            if (!File.Exists(args[0])) {
                File.Create(args[0]);
            }


            if(double.TryParse(args[1],out double dbl)) {
                Console.WriteLine($"arg[1]={dbl.ToString()}");
            }
            if (int.TryParse(args[2], out int inte)) {
                Console.WriteLine($"arg[2]={inte.ToString()}");
            }

            Console.ReadLine();
            Console.WriteLine("Hello World!");
        }
    }
Run Code Online (Sandbox Code Playgroud)

到目前为止,我尝试了2>重定向异常,但它创建了一个0kb文件。

PS另外,我不确定我是否正确运行。我.Net Core Application使用过:
来源: https: //ss64.com/nt/start.html
START "title" [/D path] [options] "command" [parameters]
在我们的例子中,dotnet命令适合哪里?您通常从终端开始,
dotnet [appname].dll但在这里您将其放在路径和名称之后dll

Ada*_*mon 5

在我们的例子中,dotnet 命令适合哪里?您通常从终端使用 dotnet [appname].dll 启动,但在这里您将它放在路径和 dll 名称之后?

除了其他问题之外,

start "Dotnet Test" D:\adi\NET\Bench\Bench\bin\Debug\netcoreapp2.1\Bench.dll dotnet %1 %2 %3 2>output.txt

应该

start "Dotnet Test" dotnet D:\adi\NET\Bench\Bench\bin\Debug\netcoreapp2.1\Bench.dll %1 %2 %3 2>output.txt

你不能运行DLL,你想运行dotnet来执行DLL中包含的代码。

我也不确定我是否正确运行 .Net Core 应用程序。

我怀疑您由于上述误解而尝试使用start 。要在脚本中运行您的应用程序,您需要做的是:

dotnet D:\adi\NET\Bench\Bench\bin\Debug\netcoreapp2.1\Bench.dll %1 %2 %3 2>output.txt

仅当您想要在单独的窗口中运行 .NET Core 应用程序(与脚本的其余部分并行)时,才需要启动。这就是命令的内容

start "Dotnet Test" dotnet D:\adi\NET\Bench\Bench\bin\Debug\netcoreapp2.1\Bench.dll %1 %2 %3 2^>output.txt

应该做。我写“应该”是因为根据这个讨论应该有效,但实际上却行不通。

我只能使用解决方法使其工作。我创建了一个单独的批处理脚本runapp.cmd,其中包含以下内容:

dotnet [appname].dll %1 %2 %3 2>error.txt & exit

开始调用这个脚本:

start "Dotnet Test" runapp.cmd %1 %2 %3