How can I run this command in c#
telalertc -i bilal -m "Test Message"
string command = "telalertc -i bilal -m "Test";
System.Diagnostics.ProcessStartInfo procStartInfo =
new System.Diagnostics.ProcessStartInfo("cmd", " /c " + command);
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
procStartInfo.Domain = "*.*.*.*";
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
string result = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();
Console.WriteLine(result);
How can I run this command using console application C#
If you want to start telalertc start it, not cmd:
System.Diagnostics.ProcessStartInfo procStartInfo = new ProcessStartInfo() {
FileName = "telalertc",
Arguments = " -i bilal -m \"Test Message\"",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true,
Domain = "*.*.*.*" // do you really want this?
};
// Wrap IDisposable into using
using (var proc = System.Diagnostics.Process.Start(procStartInfo)) {
// First wait for completeness
proc.WaitForExit();
// Then read results
string result = proc.StandardOutput.ReadToEnd();
Console.WriteLine(result);
}
Related
I am trying to execute netstat command from my C# code and get "File Not Found" error.
Do I have to specify where "netstat.exe" is?
If so, how would I do it if (hypothetically) netstat and findstr are in two different folders?
Process cmd = new Process();
cmd.StartInfo.FileName = "netstat -a | findstr 5840";
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.UseShellExecute = false;
try
{
cmd.Start();
}
catch (System.ComponentModel.Win32Exception ex)
{
Console.WriteLine(ex.Message);
}
cmd.StandardInput.Flush();
cmd.StandardInput.Close();
cmd.WaitForExit();
StreamReader reader = cmd.StandardOutput;
string output = reader.ReadToEnd();
Console.WriteLine(output);
This is what fixed the problem:
// create the ProcessStartInfo using "cmd" as the program to be run and "/c " as the parameters.
// /c tells cmd that we want it to execute the command that follows and then exit.
string command = "netstat -a | findstr " + sTCPPort;
ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd", "/c " + command);
// The following commands are needed to redirect the standard output.
// This means that it will be redirected to the Process.StandardOutput StreamReader.
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
// Do not create the black window.
procStartInfo.CreateNoWindow = true;
// Now we create a process, assign its ProcessStartInfo and start it
Process proc = new Process();
proc.StartInfo = procStartInfo;
proc.Start();
// Get the output into a string
result = proc.StandardOutput.ReadToEnd();
Without seeing the exact it is hard to know the exact issue, but it looks like you may need to cmd.exe and pass your command to it as an argument.
I'm trying to execute wmic command on C# and get the output, but the function is only returning first line and the command which is not running.
Code:
private static String wimc(String cmd)
{
var psi = new ProcessStartInfo("wmic");
psi.Arguments = #"shadowcopy call create Volume='C:\'";
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
var p = Process.Start(psi);
p.WaitForExit();
String output = p.StandardOutput.ReadToEnd();
return output;
}
Output on C#:
Executing (Win32_ShadowCopy)->create()
Only show first line and command not working
Cmd output(expected)
Executing (Win32_ShadowCopy)->create() Method execution successful. Out Parameters: instance of __PARAMETERS {
ReturnValue = 0;
ShadowID = "{B2FDCFDE-7C48-4F96-9648-9A15DB89506C}";
};
shadowcopy on cmd was created with sucess
For redirecting wmic to the console output you need to add /OUTPUT:STDOUT to your arguments.
And of course you will need to run your C# application as administrator.
var psi = new ProcessStartInfo("wmic");
psi.Arguments = #"/OUTPUT:STDOUT shadowcopy call create Volume='C:\'";
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
var p = Process.Start(psi);
p.WaitForExit();
String output = p.StandardOutput.ReadToEnd();
String errOutput = p.StandardError.ReadToEnd();
Here is what worked for me.
System.Diagnostics.ProcessStartInfo usbDevicesInfo = new System.Diagnostics.ProcessStartInfo("wmic", "path CIM_USBDevice get Caption");
usbDevicesInfo.RedirectStandardOutput = true;
usbDevicesInfo.UseShellExecute = false;
usbDevicesInfo.CreateNoWindow = true;
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo = usbDevicesInfo;
process.Start();
process.WaitForExit();
Console.WriteLine("ExitCode: " + process.ExitCode.ToString() + "\n");
result = process.StandardOutput.ReadToEnd();
Console.WriteLine(result);
I'm trying to run cmd command as administrator. But the CMD window closes unexpectedly. If CMD window stays I can see the error. I tried to use process.WaitForExit();
I am trying to run the code zipalign -v 4 your_project_name-unaligned.apk your_project_name.apk as administrator.
Here is my code.
//The command that we want to run
string subCommand = zipAlignPath + " -v 4 ";
//The arguments to the command that we want to run
string subCommandArgs = apkPath + " release_aligned.apk";
//I am wrapping everything in a CMD /K command so that I can see the output and so that it stays up after executing
//Note: arguments in the sub command need to have their backslashes escaped which is taken care of below
string subCommandFinal = #"cmd /K \""" + subCommand.Replace(#"\", #"\\") + " " + subCommandArgs.Replace(#"\", #"\\") + #"\""";
//Run the runas command directly
ProcessStartInfo procStartInfo = new ProcessStartInfo("runas.exe");
//Create our arguments
string finalArgs = #"/env /user:Administrator """ + subCommandFinal + #"""";
procStartInfo.Arguments = finalArgs;
//command contains the command to be executed in cmd
using (System.Diagnostics.Process proc = new System.Diagnostics.Process())
{
proc.StartInfo = procStartInfo;
proc.Start();
}
Is there a way to keep the CMD window running/showing?
You are starting a process from the runas.exe executable file. That's not how to elevate a process.
Instead you need to use shell execute to start your excutable, but use the runas verb. Along these lines:
ProcessStartInfo psi = new ProcessStartInfo(...); // your command here
psi.UseShellExecute = true;
psi.Verb = "runas";
Process.Start(psi);
Capture the output(s) from your process:
proc.StartInfo = procStartInfo;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.Start()
// string output = proc.StandardOutput.ReadToEnd();
string error = proc.StandardError.ReadToEnd();
proc.WaitForExit();
Then do something with the output.
Note: you shouldn't try to synchronously read both streams at the same time, as there's a deadlock issue. You can either add asyncronous reading for one or both, or just switch back and forth until you're done troubleshooting.
The following method actually works...
private void runCMDFile()
{
string path = #"C:\Users\username\Desktop\yourFile.cmd";
Process proc = new Process();
proc.StartInfo.FileName = path;
proc.StartInfo.UseShellExecute = true;
proc.StartInfo.CreateNoWindow = false;
proc.StartInfo.RedirectStandardOutput = false;
proc.StartInfo.Verb = "runas";
proc.Start();
proc.WaitForExit();
}
I am trying to open command line in the c folder, from C#.
the expectation is to see this in the command line window:
C:>
but instead i am getting a blank cmd window.
this is the code:
var startInfo = new System.Diagnostics.ProcessStartInfo
{
WorkingDirectory = #"c:\",
WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal,
FileName = "cmd.exe",
RedirectStandardInput = true,
UseShellExecute = false
};
Console.ReadKey();
WaitForExit is what you are looking for.
EDIT:
var startInfo = new System.Diagnostics.ProcessStartInfo
{
WorkingDirectory = #"c:\",
WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal,
FileName = "cmd.exe",
RedirectStandardInput = true,
UseShellExecute = false
};
Process p = new Process();
p.StartInfo = startInfo;
p.Start();
p.WaitForExit();
This will halt the execution of all statements after WaitForExit. The time you close the command window, the statements following WaitForExit will be executed.
i think you're looking for this
first we'll create a process for CMD.exe and then passes "/K cd \". "/K" will "CMD.exe" to receive parameter and stay open, while "cd \" will take us to "C:/" which is your requirement
System.Diagnostics.Process.Start("CMD.exe", "/K \"cd /\"");
Console.ReadKey();
I believe if you use the /K Argument when executing the command, you should have a command prompt running at C:\
ProcessStartInfo startinfo = new ProcessStartInfo();
startinfo.FileName = "cmd.exe";
startinfo.WorkingDirectory = #"C:\";
startinfo.Arguments = "/K";
startinfo.UseShellExecute = false;
Process.Start(startinfo);
Or
Process command = new Process();
command.StartInfo.UseShellExecute = false;
command.StartInfo.WorkingDirectory = #"C:\";
command.StartInfo.Arguments = "/K";
command.StartInfo.FileName = "cmd.exe";
command.Start();
The /K argument executes the cmd.exe command and keeps the window open :)
When I run he following command from the command line it works fine.
COPY "C:\Windows\System32\winevt\Logs\Application.evtx" "C:\ProgramData\MyCompany\Support\Logs\Application.evtx"
But I want to run it using the following in C#
System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + CurrentCommand);
StreamReader.procStartInfo.RedirectStandardOutput = true;
procStartInfo.RedirectStandardError = true;
procStartInfo.UseShellExecute = false;
// Do not create the black window.
procStartInfo.CreateNoWindow = true;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
while (!proc.StandardError.EndOfStream)
{
sError = proc.StandardError.ReadLine();
//System.Windows.Forms.MessageBox.Show("ERROR: " + sError);
}
proc.WaitForExit();
// Get the output into a string
sResult = proc.StandardOutput.ReadToEnd();
sResult returns the following error:
The system cannot find the path specified
Why is this?
I guess you need to "escape" the command parameters for the cmd /c argument like
ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd", string.Format("/c \"{0}\"", CurrentCommand));