How to pass command line argument in .net windows application - c#

i want to pass a argument in c#.net to a console application i tried ProcessStartInfo
but that can be used for immediate run of an application ... but i want to set the arguments for the application which will run at scheduled time

Use the arguments propery to pass command line arguments
http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.arguments.aspx
Example:
var info = new System.Diagnostics.ProcessStartInfo();
info.FileName = "cmd.exe";
info.Arguments = "/C";
info.UseShellExecute = true;
var process = new System.Diagnostics.Process();
process.StartInfo = info;
process.Start();
process.WaitForExit();

Related

How to start a c# process as another user?

I want to start a c# process as another user.
I know the verb "runas" but that only accepts admin user creds.
//This code actually meant to restart the appplication with specific args
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = true;
startInfo.WorkingDirectory = Environment.CurrentDirectory;
startInfo.FileName = Application.ExecutablePath;
startInfo.Arguments = args;
startInfo.Verb = "runas"; //this line is the question
Process p = Process.Start(startInfo);
I want to achieve the same thing but also with non-admin users.

C# Run console application from another app in a new console window

The second app is a console application and I want to see it's output window.
I know how to use Process.Start() but it doesn't show the console window for the app.
This is what I have tried:
Process.Start("MyApp.exe", "arg1 arg2");
So how to do it?
Perhapse this helps:
ProcessStartInfo info = new ProcessStartInfo(fileName, arg);
info.CreateNoWindow = false;
info.UseShellExecute = true;
Process processChild = Process.Start(info);
I figured it out. I have to run cmd command with /k argument (to keep the console window open) and then my whole command-line:
var command = "MyApp.exe arg1 arg2";
ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd", "/k " + command);
processStartInfo.UseShellExecute = false;
Process process = new Process();
process.StartInfo = processStartInfo;
process.Start();
//In case you need the output. But you have to wait enough for the output
//string text = process.StandardOutput.ReadToEnd();

How do I get the output from handbrakecli --scan?

I am working on a c# program to loop over my Windows Media Center recorded TV shows (.wtv) and convert them using the handbrake cli. I just got everything to work now and I wanted to also utilize the --scan function so that I can customize the audio and video arguments based on the input file rather then set a static.
This is what I have so far for the scan but I can't seem to find where the data is that prints out to the console window.
var p = new Process();
var pSI = new ProcessStartInfo();
pSI.RedirectStandardOutput = true;
pSI.UseShellExecute = false;
pSI.FileName = HandBrakeLocation;
pSI.Arguments = string.Concat(#"--scan -i ", '"', inputFile, '"');
pSI.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo = pSI;
p.Start();
var stdout = p.StandardOutput;//streamreader
p.WaitForExit();
I thought that perhaps the p.StandardOutput would send the console output to the stdout StreamReader variable, but I could not find it anywhere inside the object. What am i missing?
Thanks for you time and assistance.
You can read the program output using the StandardOutput property of the process (which is a stream):
var output = stdout.ReadToEnd();
p.WaitForExit();
More info can be found on MSDN: https://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput%28v=vs.110%29.aspx

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>

command prompt c# [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Redirect Standard Output Efficiently in .NET
Capturing console output from a .NET application (C#)
I know how to execute something like this:
SomeEXE inputfile.txt
in the command prompt via C#.
The problem I am having is that SomeEXE opens another command prompt where it writes the outputs given inputfile.txt.
Is it generally possible to obtain these outputs? Thanks.
Here is my current code:
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C SomeEXE inputfile.txt";
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
process.StartInfo = startInfo;
process.Start();
// Now use streams to capture the output
StreamReader outputReader = process.StandardOutput;
process.WaitForExit();
String line = outputReader.ReadToEnd();
ProcessStartInfo processStartInfo = new processStartInfo("SomeEXE", "inputfile.txt");
processStartInfo.UseShellExecute = false;
processStartInfo.ErrorDialog = false;
// Here is where you grab the output:
processStartInfo.RedirectStandardOutput = true;
Process process = new Process {
StartInfo = processStartInfo
};
process.Start();
// Now use streams to capture the output
StreamReader outputReader = process.StandardOutput;
process.WaitForExit();
Now you can read the outputStream as necessary.
I am guessing this is what you mean. Also, here are the docs on RedirectStandardOutput
Also, if you know the path to the file that was generated (assuming the SomeEXE wrote to another file) you can use File.Open to access its contents after SomeEXE has executed (remember to wait until after otherwise SomeEXE may still have a handle on the file making it difficult to read it).

Categories