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 Answer
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 !
for /f "tokens=* delims=" %%# in ('csprogram.exe') do set "output=%%#"