WPF + Showing ouput of bat file in TextBlock - c#

I am trying to execute a bat file through my WPF application on a button click.
I want the output of the batch file to be displayed in a TextBlock(with vertical scroll) of WPF application.
I am able to execute a bat file using Process.Start
Here is my Code
Process process = new Process();
process.StartInfo.FileName = #"C:\bin\run.bat";
process.StartInfo.Arguments = #"-X";
process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
process.Start();
process.WaitForExit();
How to proceed further?
Thanks

I guess I will flesh out my comment with an answer. You need to redirect the output of your bat file, you do that by using Process.RedirectStandardOutput. Taking your code and the MSDN Library page's code will give you something like this.
Process process = new Process();
process.StartInfo.FileName = #"C:\bin\run.bat";
process.StartInfo.Arguments = #"-X";
process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
process.StartInfo.UseShellExecute = false; //Changed Line
process.StartInfo.RedirectStandardOutput = true; //Changed Line
process.Start();
string output = process.StandardOutput.ReadToEnd(); //Changed Line
process.WaitForExit(); //Moved Line

you need to redirect standard output to your text file using the gtr symbol ">". e.g
command>myfile.extension
and the opposite executes the commands in a file one line at a time e.g
command

Related

Unity C# Encoding Type

I create simple unity application, there is button that give user print a picture. When button is clicked, it will run a command in cmd but I get this message,
'cd' is not recognized as an internal or external command,
operable program or batch file.
I search the web, it says I need to change encoding type but I have do it in Notepad++ (I change it from UTF-8 BOM to ANSI) but it still no work.
Here is my code, in case I am wrong.
string fullCommand = "rundll32 C:/WINDOWS/system32/shimgvw.dll,ImageView_PrintTo " + filePath + printerName
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.CreateNoWindow = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.Arguments = "/c " + fullCommand;
process.Start();
This is my reference
How to direct printing of photo or text using Unity without preview
I expect it to print the expected file but it do not. I hope someone can guide me. Thank you for helping.
Cheers
Kev

C# starts some cmd commands but other doesn't

My windows forms app triggers an event:
using System.Diagnostics;
string strCmdText = "'/C ping server1.example.com > C:\\Users\\myusername\\Desktop\\1\\a.txt";
Process.Start("cmd.exe", strCmdText);
When executing, cmd.exe is getting spawned, runs for a while, the output is not displayed, but it is present in the redirected 1.txt file.
However, I need to run query command:
using System.Diagnostics;
string strCmdText = "'/C query user /server:server1.example.com > C:\\Users\\myusername\\Desktop\\1\\a.txt";
Process.Start("cmd.exe", strCmdText);
When executing, it spawns a cmd.exe but just for 1 second, then it dissapears, and the output is not present in the 1.txt file.
Is there any way to see what the query command does before it disappears, like keep it open when executing? Maybe is something interesting in there.
Or, am I doing something wrong? Maybe I need to run the command otherwise?
This way:
string outputProcess = "";
string errorProcess = "";
using (Process process = new Process())
{
process.StartInfo.FileName = yourPath;
process.StartInfo.Arguments = yourArguments;
process.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.Start();
outputProcess = process.StandardOutput.ReadToEnd();
errorProcess = process.StandardError.ReadToEnd();
process.WaitForExit();
}
if you really want to run the code as yours. just replace the "CMD" to "CMD /k"

WPF Run CMD line with simple argument, print output - without batch file

I'm having some difficulty doing this without using a batch file. What I want to do is when a button is clicked, run the command line with a simple argument that I specify.
Here's my code so far:
ProcessStartInfo startInfo = new ProcessStartInfo("cmd.exe");
startInfo.WindowStyle = ProcessWindowStyle.Normal;
startInfo.UseShellExecute = true;
startInfo.Arguments = "dir";
Process.Start(startInfo);
string output = Process.StandardOutput.ReadToEnd();
txtblkOutput.Text = output;
However, this just opens a cmd window and nothing happens. The text box remains blank.
However I can do this:
var process = new Process();
process.StartInfo.FileName = "C:/Users/user/Documents/SUB-20 Tool/commands.bat";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
string output = process.StandardOutput.ReadToEnd();
txtblkOutput.Text = output;
Inside the batch file it just says dir. And this works, I get the output sent to my textbox.
Why does this work only with a batch file? Can I do this without it, with just using the argument property?
This is the excepted behaviour. When you execute cmd.exe with the argument dir, it does not execute the command.
As an exemple, see the screenshot below :
The correct way to execute a command in the arguments is the following :
cmd.exe /C <command>

in c#, can't suppress every line of a process

I'm trying to read the output of a process to string. For some reason, it sort of looks like the one line in the middle of the output seems to get outputted (ie, it's displayed on the screen, and NOT saved to the string).
string strOutput = "";
Process process = new Process();
process.StartInfo.FileName = "nslookup";
process.StartInfo.Arguments = "-type=mx uic.edu";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
strOutput = process.StandardOutput.ReadToEnd();
process.WaitForExit();
Console.WriteLine("xxxxxxxxxxxxxxxxxxx");
Console.WriteLine(strOutput);
Console.WriteLine("yyyyyyyyyyyyyyyyyyy");
I get output that looks like this:
Non-Authoritative answer:
xxxxxxxxxxxxxxxxxxxx
Server: aaa.myserver.com
Address: 111.222.111.222
uic.edu MX preference = 10, mail exchanger - ...
...
yyyyyyyyyyyyyyyyyyyy
When I run the command via command line, "Non-Authoritative answer:" comes after "Address: ..."
Can someone explain why it's outputted, and not stored as part of the string? I'm probably missing something obvious, but I'm boggled.
Thanks
That line is probably going to STDERR rather than STDOUT. Try redirecting standard error as well as standard output.
process.StartInfo.RedirectStandardError = true;

Run "tf.exe status" in C# and save the result

I´m trying to create a small console app in c#. I want to run the program and save all pending changes in TFS to a .txt file. But I cant get the arguments to work. Can someone help me?
Here is my code i haved done so far:
string argument = "#tf.exe status /collection:http://tiffany:8080/tfs/ /user:* /format:detailed >c:\\Status\\Detailed.txt";
try
{
Process process = new Process();
process.StartInfo.Arguments = "#call" + " " + "C:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\Common7\\Tools\\VsDevCmd.bat";
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Verb = "runas";
process.StartInfo.Arguments = argument;
process.StartInfo.CreateNoWindow = false;
process.Start();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Console.ReadKey();
}
aI'm not really sure that I understand what you're trying to call, exactly.
Let's assume you want to run the following command line from a C# application, as if you would call it from a command line:
tf.exe status /collection:http://tiffany:8080/tfs/ /user:* /format:detailed >c:\\Status\\Detailed.txt"
I would use this code:
string arguments = #"/C tf.exe status /collection:http://tiffany:8080/tfs/ /user:* /format:detailed >c:\\Status\\Detailed.txt";
this.process = new Process();
this.process.StartInfo.FileName = #"cmd.exe";
this.process.StartInfo.Arguments = arguments;
this.process.Start();
Edit:
If that's all your console app does, why not consider creating a batch (.BAT / .CMD) file instead of a C# application?
Instead of running a command line tool you could leverage the TFS API.
There are many articles out there, e.g. Code project article on topic
and
Sample code directly from the MSDN
I suppose you have to read standard error and output from process started:
Process process = new Process();
process.StartInfo.Arguments = #"status PATH /recursive";
process.StartInfo.FileName = "tf.exe";
process.StartInfo.CreateNoWindow = false;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
var st = process.StandardOutput.ReadToEnd();
var err = process.StandardError.ReadToEnd();
But parsing tf output is not easy and I'd like to suggest to use TFS API as #Mare said
You do not need to create an application in C # to save in a text file. Just use the parameters (...) > [file name].txt at the end of the command.
The ">" symbol send the result of any command to a file.

Categories