Execute commandline commands in c# [duplicate] - c#

This question already has answers here:
Execute multiple command lines with the same process using .NET
(10 answers)
Closed 3 years ago.
I need to run cmd commands in a c# program im writing, the only solutions ive found make a new command line instance for every command, but i need them to be executed in the same instance
Run Command Prompt Commands
public static void UpdateDeviceData()
{
Process.Start("CMD.exe",
String.Format("client_commandline.exe setdeviceposition 0 {0} {1} {2}", LPos.X, LPos.Y, LPos.Z));
}

i found a solution, its this:
Process CmdPro;
ProcessStartInfo startInfo;
startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = false;
startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardOutput = false;
startInfo.RedirectStandardError = false;
startInfo.FileName = "CMD.exe";
CmdPro = new Process();
CmdPro.StartInfo = startInfo; CmdPro.Start();
Console.SetOut(CmdPro.StandardInput);

Related

CMD Command doesn't execute [duplicate]

This question already has an answer here:
Run process as administrator from a non-admin application
(1 answer)
Closed 1 year ago.
I'm trying to run this command in cmd from c# but for some reason it doesn't work. But when I change my Argument to "/C ipconfig" it work perfectly. I don't understand why. What am I doing wrong?
Here is my code:
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = $"/C wbadmin start backup - backupTarget:{TargetBackupDrive}: -include:{LocationOs}: -allcritical - quiet";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.Verb = "runas";
process.Start();
CmdOutput = process.StandardOutput.ReadToEnd();
process.WaitForExit();
The verb "runas" does not work when UseShellExecute is set to false as it is pointed out here: Set ProcessStartInfo.EnvironmentVariables when Verb="runas"
You could either provide admin credentials with the UserName and Password properties our set UseShellExecute to true and execute your command in the shell.

Is there a way to have c# open another program using events? [duplicate]

This question already has answers here:
Launching an application (.EXE) from C#?
(9 answers)
Closed 4 years ago.
For example: if one program had an alert, another separate program pop up when that alarm goes off?
Yes, you can launch another program using C# code. Here's how you do that:
static void LaunchProgram()
{
// Path to program
const string ex2 = "C:\\Dir";
// Use ProcessStartInfo class
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
//File name of program to launch
startInfo.FileName = "program.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
try
{
// Start the process with the info we specified.
// Call WaitForExit and then the using statement will close.
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
}
}
catch
{
// Log error.
}
}

Execute the CutyCapt commands using c# code [duplicate]

This question already has answers here:
Run Command Prompt Commands
(16 answers)
Closed 8 years ago.
I am new to C# code, I need to run the below commands by using C# code, Could any one give me the full code for this one?
Command :
C:\\Srinivasa\\VisualStudioProject\\CutyCapt_Pdf_Code\\release\\CutyCapt.exe
Arguments :
--url=file:///C:/Users/UPPALASX/Desktop/New%20folder/ResearchMap.html
--out=Out_Embeded_RM.png
--min-width=800
--min-height=10000
Try This:
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "C:\\Srinivasa\\VisualStudioProject\\CutyCapt_Pdf_Code\\
release\\CutyCapt.exe";
startInfo.Arguments = "--url=\'C:/Users/UPPALASX/Desktop/New
folder/ResearchMap.html\' --out=Out_Embeded_RM.png --min-width=800
--min-height=10000";
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
Look at Process.Start and Process.StartInfo

Run an another cmd [duplicate]

This question already has answers here:
Run console application from other console app
(6 answers)
Closed 9 years ago.
I make a C# console program and I want to execute some batch treatments in an other cosole.
So, I have the main program which write in the console and at a certain moment I want to execute batch treatment in an other one.
I know how execute batch treatment in the main console but I want to do that in an other one, that is my question.
How can I make that ?
EDIT :
I use a StreaWriter to write in the console like that :
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "cmd.exe";
startInfo.RedirectStandardInput = true;
startInfo.UseShellExecute = false;
process.StartInfo = startInfo;
process.Start();
using (StreamWriter writer = process.StandardInput)
{
if (writer.BaseStream.CanWrite)
{
// commands...
}
}
Use Process.Start:
Process.Start("cmd.exe", "yourcommandhere");

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