Search code examples
c#windowsbatch-filereturnflags

returning the output of a C# program called by a batch file to that batch file without using temp file read/writes


what I'm doing

This first section describes why I believe this question is not a duplicate. I'm trying to get some output of a C# windows forms application into a variable in batch. There are some caveats however. Due to the requirements of this project (it is for work), I can't make temp files to simply write to from C# and read from batch. This is because it would present a security risk for the whole application and that is unacceptable. The other caveat is that the C# program is called and started from the batch file with the START /WAIT command. To clarify, I am not the only author of this code, I made a good portion of it, debugged and unit tested it, but this code is not entirely mine. Thus "just refactor it and use something easier" is not an option this late into development. The code works well for its intent, I just need to solve this problem. These caveats, along with the fact that the C# program is NOT a console application but a windows forms application, leads me to believe this question is not a duplicate of other questions on this topic. This is because I understand how to get batch to try reading the program output (what the other questions were about) but I don't understand what to put in the C# program to allow batch to succeed in reading it (my question).

What is the application for?

I won't go into details because that probably isn't legal but the purpose of the C# program and the batch program is basically to setup windows machines for our network service by configuring various windows options. the C# portion is needed for passwords, which can't be done in batch because there is no way to have special character passwords in a variable (batch interprets them as commands instead of literals and there is no easy way around this). Since it is a requirement that our users be able to have special characters in their passwords, we couldn't use batch for that portion of the program. The problem is that I need to know when the C# program is abruptly closed. As a windows form, it has the red X option that can be selected before the password is set. The batch file needs to know if the C# program was closed early so it can report a warning to the user that the password was not set. The problem is that I can't figure out how to get output from the C# application into batch to display that warning. If I can't flag the warning condition, I can't display the warning.

my research

I found this question: Assign output of a program to a variable

Which gives me a good starting point. Instead of calling the application generically however, I used the start command because I wanted the batch file to wait before it continues on with the rest of the process. My batch code when ran from the CLI (assume I'm in the directory of the executable) is this:

FOR /F "tokens=*" %A IN ('start /WAIT "" "<TheApplicationName>.exe"') DO (echo output line: "%A")

Obviously in a batch script the extra percent signs need to be added but for the CLI itself this is fine.

The problem is that I can't figure out what command in C# I need to execute to get something to be read by the above batch code. I've tried Console.Writeline("something") in my C# main class but it didn't cause anything to echo out when I ran the program through the batch code. I'm pretty sure the batch code is correct because it does run the application.

The steps I take to run the C# program are as follows:

  1. cd into the right directory
  2. run the above batch code
  3. check that the application runs in its entirity like it is supposed to (it does)
  4. Observe that nothing from the program is echoed out, not even a blank line (the DO portion of the FOR loop never seems to output anything)

So my question is this: what command in C# do I need to use to get the batch code to echo out SOMETHING from my program. I know how to use the string once I get it, I just need to get it first. I feel like either, it can't be done because it is a windows form application, it can be done but I'm just not smart, or that the read operation is happening too fast (before anything is printed) and thus I get nothing. I can't find any examples of what commands the batch code can 'read' so I assumed it was somehow related to console output or print commands.

Suggestions for question improvement are appreciated


Solution

  • for /f executes the code in the do clause for each non empty line (see also for /? output about eol handling) in the output of the indicated command. The problem in your code is that the output being processes is that of the start command (start command outputs nothing), not your application's output.

    As the for /f will wait for the executed command to end (the code in the do clause is not executed until all the output has been retrieved) the start /wait is not only not needed but the soruce of the problems.

    Just remove the start /wait from the command to process the application's output.