I have a WPF app. I want to use the same app in command line also. I did the following if args.Length > 0
AttachConsole(-1);
Console.WriteLine("Start");
Console.WriteLine("Stop");
FreeConsole();
It give me o/p like that
C:\Work>TestWriteCLI.exe -h
C:\Work>Start
Stop
The problem is that it does not returns to next command prompt until i press and Enter.
I tried AllocConsole() ebut it create a new console which i don't want. I want the o/p should
be like this:
C:\Work>TestWriteCLI.exe -h
Start
Stop
C:\Work>
Related
I have a simple c# program that expects command line input (from Console.ReadLine()) when ran. There's a lot of inputs I have to provide, so I was wondering how I could automate this process. I currently have a shell script that attempts to do this, but it's not working.
#!/bin/sh
dotnet run #run the program
1 #input first argument (this failed so I tried echo 1 instead but no luck)
# <- rest of command line inputs on each line
Not really familiar with shell scripts, and I'm not fixed on this solution if you had another solution in mind. My OS is MacOS.
OK, so what I tried was this:
I made a little mcve-Console App: (dotnet 6)
var input = Console.ReadLine();
while( !string.IsNullOrEmpty(input) )
{
Console.WriteLine("User input: {0}", input);
input = Console.ReadLine();
}
Then I made a little input.txt
This is a Text.
This is another Text.
1
2
3
This is the final Text.
and finally run.sh as follows:
#!/bin/sh
dotnet run < "input.txt"
Output was
User input: This is a Text.
User input: This is another Text.
User input: 1
User input: 2
User input: 3
User input: This is the final Text.
/bin/sh is linked to dash shell in my case.
If in run.sh you replace "input.txt" with "$1", then you can call it with ./run.sh whateveryourinputfileis.txt to make a little more flexible.
I want to capture output of a child process spawned from a parent process.
For example, when the parent process runs on a command window like below
c:>parent.exe
it spawns a child process in a separate window.
I tried like below to capture the output (including Java exception), it cannot capture output from child process
c:>parent.exe > error.log
spawned a new process on a new window
Any idea?
You can do this very simple with powershell.It's available on every windows-system.
Then you can start you programm with the command start-process documentation
Here you can redirect stdout an stderr very easy to a file.
If you want to redirect the stdout to a variable you can run your programm like that.
open cmd
type powershell
$outFromParent = $(parent.exe)
Or if you want to redirect to a file
PS c:> parent.exe > theOutFile.txt
Update
If this not work then you can try the folowing.
PS c:\>$out = & parant.exe
PS c:\>$out
OR
PS c:\>Write-Host $out
If this not works you can try the Start-Process. See the example.
Or look at here
PS C:\> Start-Process -FilePath "parant.exe" -RedirectStandardInput "testin.txt" -RedirectStandardOutput "testout.txt" -RedirectStandardError "testerror.txt"
Note if you start an application that need elevated rights then all of these methods only work if you use an andmistrator powershell.
Hope this helps.
There are 2 PCs(server & node). The Selenium hub is up & running. The notifications are seen in its cmd window.
Now, I'm trying to set up another PC as a Selenium node. To do that I need to run 2 commands from the server PC command prompt.It works when done manually.Failing to do so programatically.
Here is what I have so far.
private static void StartSeleniumNode()
{
string Command1 = "/C cmdkey.exe /add:ABCDES181 /user:abc /pass:abc#123 & ";
string Command2 = "psexec.exe \\ABCDES181 -i -w D:\\Selenium java -jar selenium-server-standalone-2.47.1.jar -role node -hub http://someip:4444/grid/register";
Process.Start(cmd.exe, Command1 + Command2);
}
When run, a cmd window just pops up and closes. There would be a notification if a node is registered, but nothing of that sort here. I think it is the syntax to run 2 commands that is the issue here.
The way to tell cmd to run multiple commands is to chain them using &&.
For ex, you could get your command prompt to do this:
echo hello && echo world
In your case, try using this statement:
Process.Start(Constants.CommandPrompt, string.Format("{0} && {1}", Command1,Command2));
So there is a program someone wrote (that I don't have access to) that was written in C#, in which when I open it, it brings up a command prompt, asks several questions, and then returns an output.
What I want to do it write a batch file to automate entering all the arguments manually but nothing has really worked for me thus far. I tried "program.exe arg1 arg2.." in the command prompt and reading about the windows commands (I checked out ss64) but nothing seems to work.
So to summarize what happens is:
1) I open the program (a .exe file) in the command prompt (or click on it) where it asks me to enter a value or filename
http://i.stack.imgur.com/bZsSi.png
2) I press enter to continue to the next question and the command asks me to answer another question (unless I finished the last one, in which case the program finishes executing and then closes).
http://i.stack.imgur.com/nqJ5M.png
Now, how would I go about making a batch file that enters SWAIN.dat, n, 1000, etc... automatically into this program? Again, I don't have access to the original program. i only know it was written in C#.
Thank you very much.
You could create a VB script -
set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "cmd"
WScript.Sleep 100
WshShell.AppActivate "C:\Windows\system32\cmd.exe"
WScript.Sleep 100
WshShell.SendKeys "program.exe{ENTER}"
WScript.Sleep 100
WshShell.SendKeys "SWAIN.dat{ENTER}"
WScript.Sleep 100
WshShell.SendKeys "1000{ENTER}"
etc...
You may try this:
(
echo SWAIN.dat
echo n
echo 1000
echo etc...
) | program.exe
In this article http://nagios.sourceforge.net/docs/3_0/eventhandlers.html#example they show how to get arguments from nagios to shell script. I have done this. Now in this article they show how to receive nagios parameters in .bat files. You can access nagios parameters like so:
(::echo 1: %1 2: %2 3: %3 4: %4)
In shell script I would access the variables like this: echo $1, echo $2 ect...
How do I access this variables in c# in .exe file?
It looks like Nagios just passes the arguments via command line. Assuming your C# project is just a console application, the command line arguments will be passed to Main. So the following will print 3 arguments:
class Program
{
static void Main(string[] args)
{
Console.WriteLine(args[0]); // echo $1
Console.WriteLine(args[1]); // echo $2
Console.WriteLine(args[2]); // echo $3
}
}
Note, before retrieving the command line arguments make sure to check that the indices are valid.