0

Is there any way i can output a string value from a c# application and capture it in a powershell or command variable? I have a batch command job that runs a c# application. I know i can capture an integer in %ERRORLEVEL% using Environment.Exit(). I would like to have a variable that contains a string value from the c# application. Thanks in advance,

1
  • for /f "tokens=* delims=" %%# in ('csprogram.exe') do set "output=%%#" Commented Apr 5, 2018 at 12:56

1 Answer 1

2

If in your c# code you output the variable in the console with :

Console.WriteLine(YourVar);

then in the batch file :

@echo off
for /f "delims=" %%a in ('your_c#_file.exe') do set "$var=%%a"

echo result=%$var%

Edit :

If you have multiple variable as output!

2 ways :

1 :

 @echo off
 setlocal enabledelayedexpansion

 set /a $Count=1
 for /f "delims=" %%a in ('your_c#_file.exe') do 
   set "Var!$Count!=%%a"
   set/a $Count+=1
 )     

And then you can use :

echo %Var1%
echo %Var2%
...
echo %Varx%

2:

Even better if you can output the variable from your C# code, directly like this :

name=John
lastname=Doe

then in the batch file you can directly set it like this:

@echo off
for /f "delims=" %%a in ('your_c#_file.exe') do set %%a

and then just :

echo %name%
echo %lastename%

The variable will directly have the right name !

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

2 Comments

How would this work if multiple variables are being output from the C# app?
Worked like a champ! You are awesome! Thank you so much!

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.