C# How can I pop up Command Prompt(CMD) in running Console? - c#

In c#, run new process cmd and send out command(ex.. ping 127.0.0.1 -t) to cmd
Then redirect Standardoutput to Console..
But in compile I cannot see cmd which is working in pop up window, only know that command is working on hidden state.
How can I show up command prompt like console pop up?? I want to see console window and command prompt at the same time..
If I don't redirect from cmd and only send the command(ex dir) to cmd,
still command prompt window does't showed up and only see console window..
Process proc_cmd = new Process();
ProcessStartInfo startinfo = new ProcessStartInfo();
startinfo.FileName = "cmd.exe";
startinfo.WorkingDirectory = #"c:\";
startinfo.UseShellExecute = false;//to use RedirectStandard~
startinfo.RedirectStandardInput = true;
startinfo.RedirectStandardOutput = true;
startinfo.RedirectStandardError = true;
proc_cmd.EnableRaisingEvents = false;
proc_cmd.StartInfo = startinfo;
proc_cmd.Start();
proc_cmd.StandardInput.Write(#"ping 127.0.0.1 -t" +
Environment.NewLine);
proc_cmd.StandardInput.Close();

type start in command prompt:
start
This will open up a new cmd window.

You may need something like this if you need to view the console on debug:
startInfo.CreateNoWindow = false;

Related

Process starts a new CMD instance

I have a command line application that calls a 'netsh' process and changes some IP information. The problem that I have is that every time I call my app in CMD or PowerShell, it starts a new instance of CMD(opens a new CMD window and closes it after it's finished executing)
I would like to know if there is a way to have everything happen in the same window
here is the code that starts a process:
Process p = new Process();
ProcessStartInfo psi = new ProcessStartInfo("netsh", queryStr);
p.StartInfo = psi;
p.Start();
Console.WriteLine("netsh query string is: " + "***" + queryStr + "***");
This is my first question, please don't judge too harshly
I hope I understood your question correctly. You could hide the Shell, while redirecting the ouput. For example,
Process p = new Process();
ProcessStartInfo psi = new ProcessStartInfo("netsh", queryStr);
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
p.StartInfo = psi;
p.Start();
var output = p.StandardOutput.ReadToEnd();
ProcessStartInfo.WindowStyle allows to set the state of Window when the process starts. In the current scenario, you could set it to ProcessWindowStyle.Hidden
ProcessStartInfo.UseShellExecute indicates whether to use the OS's Shell. Disabling this would help in redirecting the output.
ProcessStartInfo.RedirectStandardOutput indicates whether the output is written to StandardOutput. By setting it to true, you can redirect the output stream and use the Process.StandardOutput to read the output and display as per application design

CMD command from C# code not working (Windows)

I'm trying to run CMD from my code. This is the line that I run in my command line, and it works when I run it manually:
C:\Dev\MySite\web\Website\comparison-tool\data\ & node csvToJson.js
This is what I have in my code:
string commandText = String.Format("/C {0}{1} & node csvToJson.js", root, csvToJsonFolder);
Process.Start("CMD.exe", commandText);
commandText evaluates to /C C:\Dev\MySite\web\Website\comparison-tool\data\ & node csvToJson.js
It runs without error, but nothing seems to have happened. The command prompt doesn't open, so I can't see any errors that may have occurred. The command is supposed to result in a file being written to a particular folder, and when I run the command manually the file gets written, but when I run my code the file does not get written.
EDIT: I changed my code to this:
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Normal;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = commandText;
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
process.StartInfo = startInfo;
process.Start();
string result = process.StandardOutput.ReadToEnd();
The result is just an empty string. There is no error message or anything.
The problem was in my command text, I forgot "cd". Should have been "/C cd C:\Dev\MySite\web\Website\comparison-tool\data\ & node csvToJson.js"

How Do I Get The Command Console To Write Lines In Windows Forms Application? (Visual c#)

Basically i want the user to press a button and then the console will write all the appropriate lines for the user.
Here's the code i've written:
private void button6_Click(object sender, EventArgs e)
{
Process Cmd = new Process();
Cmd.StartInfo.FileName = #"C:\windows\system32\cmd.exe";
Cmd.Start();
StreamWriter sw = new StreamWriter(#"C:\windows\system32\cmd.exe");
{
sw.WriteLine = ("hello");
}
}
I tried StreamWriter but doesn't seem to be co-operating.
What you want can't be done, because you need to redirect StandardInput to send commands to cmd and when you do this, the cmd window will close. You still don't explain what exactly you want to archieve, but i can only think of two options:
Create a batch file with all the commands you want to execute and pass it as an argument to Process.
Redirect StandardInput and StandardOutput. This way you should take care of showing all the cmd messages. This is a bit messy and could lead you to deadlocks.
Edit
So at last, what you want is just run a console application with parameters. This is a sample using makecert:
Process ppp = new Process();
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = #"C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\makecert.exe";
psi.Arguments = "-n 'CN=TempCA' -r -sv TempCA.pvk TempCA.cer";
ppp.StartInfo = psi;
ppp.Start();
you can do something like this
Process Cmd = new Process();
Cmd.StartInfo.FileName = #"makecert.exe"; // enter the correct path
cmd.StartInfo.Argument = "" // pass your aarguemnt
Cmd.Start();

Working with the command prompt in C#

I am working on a project of remotely receiving commands from a server, but I am facing a problem when working with the command prompt locally. Once I get it working locally, then I will move to remote communication.
Problem:
I have to completely hide the console, and client must not see any response when the client is working with the command line but it will show a console for a instance and then hide it.
I had to use c# to send a command to cmd.exe and receive the result back in C#. I have done it in one way by setting the StandardOutput... and input to true.
The commands are not working. For example, D: should change the directory to D and it does, but after that, if we use dir to see the directories in D, it does not show the appropriate directories.
Here is my code:
First Method
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C " + textBoxInputCommand.Text + " >> " + " system";
process.StartInfo = startInfo;
process.Start();
Second Method
ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd", "/c " + textBoxInputCommand.Text);
procStartInfo.WorkingDirectory = #"c:\";
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = true;
procStartInfo.UseShellExecute = false;
Process proc = new Process();
proc.StartInfo = procStartInfo;
proc.Start();
string result = proc.StandardOutput.ReadToEnd();
richTextBoxCommandOutput.Text += result;
I want the program to run as administrator because the exe it generates does not run commands when it runs from the C drive.
Try not to run the commands by passing them to cmd instead write the commands passed by the client to a.bat file execute the .bat. file from your program this will probably hide your command prompt window.
You can also use process.OutputDataRecieved event handler to do anything with the output.
If you want to execute command using administrator rights you can use runascommand. It is equivalent to the sudo command in Linux. Here is a piece of code may be it will help you
var process = new Process();
var startinfo = new ProcessStartInfo(#"c:\users\Shashwat\Desktop\test.bat");
startinfo.RedirectStandardOutput = true;
startinfo.UseShellExecute = false;
process.StartInfo = startinfo;
process.OutputDataRecieved += DoSomething;
process.Start();
process.BeginOutputReadLine();
process.WaitForExit();
//Event Handler
public void DoSomething(object sener, DataReceivedEventArgs args)
{
//Do something
}
Hope it helps you.
You could hide command prompt window by adding this line of code:
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
or do not create it at all
startInfo.CreateNoWindow = true;
Here can be found a few awarding solutions:
Run Command Prompt Commands

Executing command-prompt command in C#

:)
I have a software which can be executed via command line, and now I want it to be executed directly from my C# app. Sadly, there is no error but I still can't do it. :(
The path of .exe file of the software is C:\program files\mysoftware.exe
The command I would like to input is
cd c:\program files\mysoftwareFolder
enter
mysoftware.exe d:\myfolder\file1.xxx d:\myfolder\file2.xxx -mycommand
enter
exit
The commands above work so well in the actual command prompt, but they just don't work from my C# code.
Here is the code:
Process cmdprocess = new Process();
System.Diagnostics.ProcessStartInfo startinfo = new System.Diagnostics.ProcessStartInfo();
startinfo.FileName = "cmd";
startinfo.WindowStyle = ProcessWindowStyle.Hidden;
startinfo.CreateNoWindow = true;
startinfo.RedirectStandardInput = true;
startinfo.RedirectStandardOutput = true;
startinfo.UseShellExecute = false;
cmdprocess.StartInfo = startinfo;
cmdprocess.Start();
System.IO.StreamReader sr = cmdprocess.StandardOutput;
System.IO.StreamWriter sw = cmdprocess.StandardInput;
sw.WriteLine(#"echo on");
sw.WriteLine(#"c:");
sw.WriteLine(#"cd" +#"program files\mysoftwarefolder");
sw.WriteLine(#"mysoftware.exe" +#"d:\myfolder\file1.xxx" +#"d:\myfolder\file2.xxx" +#"-mycommand");
sw.WriteLine(#"exit");
sw.Close();
sr.Close();
I guess the incorrect parts might be "startinfo.FileName = "cmd";" or the way I typed the command in the code, but I have no idea how to correct them. :(
Please tell me what I did wrong. I appreciate every answer from you! :)))
UPDATE Thank you for your helps! I tried writing the command in batch file, but it only works in debugging mode. (I forgot to tell you guys that I am developing a web service.) When I run my external project which will use this C# service, it won't work. I don't know whether I should add something to my code or not.
help meeeeee pleaseeeee (T___T)
Write these commands in a batch file and execute the batch file.
In batch file:
cd c:\program files\mysoftwareFolder
mysoftware.exe
d:\myfolder\file1.xxx
d:\myfolder\file2.xxx -mycommand
exit
Code:
Process cmdprocess = new Process();
ProcessStartInfo startinfo = new ProcessStartInfo();
startinfo.FileName = "path to batchfile.bat";
startinfo.WindowStyle = ProcessWindowStyle.Hidden;
startinfo.CreateNoWindow = true;
startinfo.RedirectStandardInput = true;
startinfo.RedirectStandardOutput = true;
startinfo.UseShellExecute = false;
cmdprocess.StartInfo = startinfo;
cmdprocess.Start();
Instead of:
startinfo.FileName = "cmd";
Directly use
startinfo.FileName = #"c:\program files\mysoftwarefolder\mysoftware.exe";
Then pass the arguments to the start info as
startinfo.Arguments = #"d:\myfolder\file1.xxx " +#"d:\myfolder\file2.xxx " +#"-mycommand";
So the whole code looks like:
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = #"c:\program files\mysoftwarefolder\mysoftware.exe";
p.StartInfo.Arguments = #"d:\myfolder\file1.xxx " +#"d:\myfolder\file2.xxx " +#"-mycommand";
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
If you need to see output from your program you can simply use the output string.
2 things: I think you have spacing problems and you're not reading the result of these commands. cmd is probably telling you ..."is not recognized as an internal or external command"
If you look at what you're throwing at cmd, it will be:
echo on
c:
cdprogram files\mysoftware folder
mysoftware.exed:\myfolder\file1.xxx
That won't work when you try it in cmd. CMD is almost certainly kicking back error messages at you, but you're never reading from sr so you'll never know it.
I'd add in some spaces and include all the paths in quotes internally like so:
sw.WriteLine(#"echo on");
sw.WriteLine(#"c:");
sw.WriteLine("cd \"program files\\mysoftwarefolder\"");
sw.WriteLine("mysoftware.exe \"d:\\myfolder\\file1.xxx\" d:\\myfolder\\file2.xxx\" -mycommand");
sw.WriteLine(#"exit");

Categories

Resources