0

NET Core Console App` from a batch script and redirect the exceptions to a file.I am also providing arguments to this script:

My script so far:

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

.NET Core Console App

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!");
        }
    }

So far i tried with 2> for redirecting exception but it creates a 0kb file.

P.S Also i am not sure if i am correctly running the .Net Core Application.I have used:
Source: https://ss64.com/nt/start.html
START "title" [/D path] [options] "command" [parameters] ,
In our case where does dotnet command fit in? You normally start from terminal with
dotnet [appname].dll but here you place it after the path and the dll name?

7
  • Shouldn't it be | or >> instead of >? Commented Aug 27, 2018 at 11:11
  • > and >> are pretty similar i didn't want to append. Commented Aug 27, 2018 at 11:13
  • Ah, okay, my bad. Commented Aug 27, 2018 at 11:14
  • 1
    Well that is how i understand you run .NET Core applicaitons using dotnet [name of dll]. Commented Aug 27, 2018 at 12:03
  • 1
    It appears that you used [name of dll] dotnet instead. Do parameters need to be preceded by --? Commented Aug 27, 2018 at 12:44

1 Answer 1

5

In our case where does dotnet command fit in? You normally start from terminal with dotnet [appname].dll but here you place it after the path and the dll name?

Apart from other problems,

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

should be

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

You cannot run a DLL, you want to run dotnet to execute the code contained in the DLL.

Also i am not sure if i am correctly running the .Net Core Application.

I suspect you are trying to use start because of the misunderstanding described above. To run your app in the script, all you need is:

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

You only need start when you want to run your .NET Core app in a separate window, parallel to the rest of the script. This is what the command

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

should do. I'm writing should because it should work according to this discussion but actually it won't.

I could make it work only using a workaround. I created a separate batch script runapp.cmd with the following content:

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

And had start call this script:

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

Sign up to request clarification or add additional context in comments.

3 Comments

I supplied the arguments in the order provided by the link ,or what i thought the link said.I myself wasn't sure if dotnet should be near the end but in that pattern isn't it in the place of "command"?
In your snippet D:\adi\NET\Bench\Bench\bin\Debug\netcoreapp2.1\Bench.dll is in the place of "command".
getting error: A fatal error was encountered. The library 'hostpolicy.dll' required to execute the application was not found in '.....path....'. Failed to run as a self-contained app. If this should be a framework-dependent app, add the ....path.....runtimeconfig.json file specifying the appropriate framework.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.