.bat file interaction with java or C#.net GUI application - c#

How to interact .bat file with java or C#.net GUI application? Here is part of my .bat code.
:Valid
echo Enter student id:
set/p "pass=>"
if NOT %pass%== UserStudentId goto FAIL
When I run the .bat file it will ask Enter student id: in command prompt, then validate the student id and do some process.
I want to pass this input from java or C#.net IDE to .bat file and continue the process. In here User will never deal with command prompt.
If there is any output from .bat , it will show in java or .net IDE also.
I wonder if it is possible since I'm new to this. TX.

When you start a process from Java or C# you can get console input and output of that process as streams. You then can simply write into the process' input stream.
It might be, though, that the batch file won't actually like that since cmd might look for actual interactive input with set /p (e.g., it fails with file redirection, if I remember correctly). A better method, if you control the batch file itself, would be the following:
if defined pass goto SkipInput
set /p pass=Enter student ID:
:SkipInput
which will use an environment variable if present and only ask the user if it isn't already set. This is much more automation-friendly, since you can just set the environment variable in the calling process and have it inherited for every process you spawn. And no hassle with emulating input as well.

Related

How do I write to a unity game's console (after build)?

I am making a game server in unity and am running it in -batchmode. I have tried Console.WriteLine and Debug.Log but neither actually print anything to the console (I'm running in a .bat file). How should I write to the console? Is there a simple way to do it or how else should I display info about the server running?
Even without explicitly defining a different log location Unity by default always produces the following log outputs (See Log Files)
PackageManager Log
Linux
~/.config/unity3d/upm.log
macOS
~/Library/Logs/Unity/upm.log
Windows
C:\Users\username\AppData\Local\Unity\Editor\upm.log
Editor Log
Linux
~/.config/unity3d/Editor.log
macOS
~/Library/Logs/Unity/Editor.log
Windows
C:\Users\username\AppData\Local\Unity\Editor\Editor.log
Player Log
Linux
~/.config/unity3d/CompanyName/ProductName/Player.log
macOS
~/Library/Logs/Company Name/Product Name/Player.log
Windows
C:\Users\username\AppData\LocalLow\CompanyName\ProductName\Player.log
So after a build you would be interested in the last one, the player log. Usually it is created the moment you start the app but not filled with content until the app is fully closed.
The exception here is defining the -logFile - which directly prints the output to stdout/the terminal - see below.
If you want to redirect the log output to a different file or the command/stdout you can use (see Command Line Arguments)
-logFile <pathname>
Specify where Unity writes the Editor or Windows/Linux/OSX standalone log file. To output to the console, specify - for the path name. On Windows, specify - option to make the output go to stdout, which is not the console by default.
And use Debug.Log as usual.
Did some searching. It would seem that you can only log to a file if you're using -batchmode using the -logFile argument and Debug.Log.
I did manage to find this custom console example though:
https://garry.tv/unity-batchmode-console if you want to put in the extra effort to be able to write to console.
Similar question here might be useful for you
https://answers.unity.com/questions/884197/redirecting-build-output-to-console-for-automated.html?_ga=2.177817063.358316465.1597205884-742321341.1597081109

How to execute batch file on the same executable as the "main" C# application

I have a given C# executable and I want to execute a batch file inside this executable. The batch file sets environment variables, and some of these operations are conditional to other environment variables (IF conditions).
I want these environment variables to be set inside the executable itself, so that the environment variables are set on the current executable.
Absolutely every method that I can find on our friend google when I write something like “C# execute batch same process” seem to be based on the method System.Diagnostics.Process method, which executes the batch file on a different process.
Thank you
I have a console application. I want this console application to interpret the commands inside that batch file so that the environment variables set within that batch file are taken into account on the console application.
But you definitely don't want to write a batch script interpreter in C#.
If you want your console app to use the environment variables assigned in the batch script, then start your executable at the end of that batch script.
Your application will then inherit that environment, and therefore those environment variables.
If you can't alter said batch script, then create a new batch script that:
Runs the original batch script, then
Runs your executable.

How to send command to a running program and get result from it?

I've an application same as command line application. I type command, it send command to server and display result on screen.
My application is an executable application on windows and has two feature that help me to easy work: record script and play script.
Now, I want to send command to running application and raise play script button to run sent command on it.
Is there any way to solve this problem in c# language?
Uh-oh. I hope I understood you correctly. I assume you have a third-party application that interacts with some server and is operated by two distinct commands, play and record. You want to write a new application that would invoke these commands somehow, and the problem is this invocation.
You can do it in C#, but the way you do it depends on the way you operate the program. I will show you some cases:
The program is a command line tool. Input is sent via command line parameters when the program is run. Output is gathered via redirections of the standard output of the program. Look at System.Diagnostics.Process.Start and its parameters, namely ProcessStartInfo which contains parameters and input/output redirection options.
The program is a console tool that is not operated via command line parameters. Use the same as above, but feed input via input redirection.
The program is a GUI program with no ability to get input in text form. This is most likely the case. Here you will have to either find a specific way to operate the program such as with an advanced technique called hooking. Or you can send commands as if a user is issuing them. Look at System.Windows.Forms.SendKeys method. It might help. How to get a result here is a big question. You can either parse the screen or monitor some state of a program or something else. No ready solution for everyone here.

How do I execute a DOS SET command from C# and have the variable persist after the exe closes?

I have a C# console executable started within a DOS command process.
I need to be able to execute DOS commands from the C# executable (specifically I need to be able to SET variables) and have the variables persist such that the rest of the DOS process can reference them.
ie:
Start DOS process
-> C# executes a SET command to set UserVariable
-> DOS process can ECHO %UserVariable%
Due to performance reasons I cannot write the set command to a dos script. In fact, I cannot have any file I/O at all.
Can anyone help?
SETX persists the environment variables. Check this: http://technet.microsoft.com/es-es/library/cc755104(v=ws.10).aspx
The problem isn't with the fact that you are calling SET from within a C# application. Even if you open a windows prompt and call SET to set a user variable, it will not persist through sessions.
Display, set, or remove CMD environment variables. Changes made with
SET will remain only for the duration of the current CMD session.
Source.
I advise you to set variables directly through .Net, anyway. You can use Environment.SetEnvironmentVariable to do this.
If you are running a C# app from a dos script and wishing to use variables set in the app afterwards from the script, I don't know how to do that for just the context of that script from within C#, the other answers here show you for the machine itself but I appreciate you need something with a less permanent scope.
A meta-programming workaround this could be to:
Call the C# app from a DOS FOR loop
From the C# app, output to the console SET commands
Use the for loop to execute the app output
The calling DOS script would look like this:
FOR /F "tokens=* delims=" %%A IN ('MyApp.exe') DO (
%%A
)
The Console output from MyApp.exe would need to be in the form:
SET UserVariable1=UserValue1
SET UserVariable2=UserValue2
And then each of the output lines would be executed by the calling FOR loop and the variables would then exist in the context of the calling script.
Use the following .NET method instead:
Environment.SetEnvironmentVariable
This is by no means an answer, but with the nature of the problem this "compromise" is the path we have chosen for the time being.
We'll be using a batch script created by the C# application which will have all the simple set commands needed which will then be executed by the CMD process that invoked the C# application.
Please feel free to add any further ideas and comments as this is not the ideal solution.
Unfortunately, so far as I can tell, this is the "solution" that is available, through at least Windows 7. I've been experimenting with various ways to set and subsequently use environment variables, and the bottom line of my research is that, if you want to use a variable, it must either be set in advance into the USER or MACHINE key, or it must be a local variable, set by script command, then tested further along in the same script.
So far as I am concerned, the third element in that enumeration, Process, is essentially useless. The only way that I can see that it might be useful is if a process spawns another process, passing its environment to the child process. That's overkill for most run-of-the-mill administrative scripts.

calling C# exe from commandline using batch file

I want to create an exe file using C# which will send emails. I want to invoke this exe using batch file. Should I create a Web form application or just a class library ? Can an exe be called using batch file from command prompt?
Please suggest.
Typically, if you want to call a application from a command line, you'd create a Console Application.
This allows your command prompt to call the application and (optionally) accept input from the console (prompt) as well as write output to the console (via the Console class).
You can read the command line arguments directly from the Main routine in the Console Application, as well.
Write a console app that takes arguments on the command line. You can then call this from script (i.e. a batch file). The arguments that you pass could contain all the elements of your email - the body may be a little large and I am not sure of the limit (if there is one) for the length of string args, but as long as the body is reasonable in size you will be ok.
I prefer writing "tools" such like this as console apps (over Windows apps) because you have so much flexibility in terms of executing the app. You can invoke it directly by typing in the cmd.exe (the command line), you can write a script to call it (as you have suggested), you can write another .NET application that can call it, etc... A Windows app has just one method of invocation.
Of course. Create the application, and store the binary exe in some folder (i.e., C:\folder).
If your exe has the name "name.exe", in your batch file, write
cd C:\folder
name
This works for all types of applications, and you can access command-line data in both, but it's easiest in a console application.
You would write:
cd C:\Users\asif\Desktop\EmailSender\ConsoleApplication1\bin\Debug\
::Space here
ConsoleApplicati‌​on1
with the new line.

Categories

Resources