Click here to Skip to main content
15,885,214 members
Please Sign up or sign in to vote.
5.00/5 (11 votes)
See more:
Hi,

I want to open a command prompt from a console application in a separate window and execute some command line in the new command prompt.

I was able to open a separate window by setting UseShellExecute=false, but not able to write any thing into it.
StandardIn has not been redirected.
am getting this exception .

C++
Process command = new Process();
ProcessStartInfo commandInfo = new ProcessStartInfo("cmd.exe");
commandInfo.WorkingDirectory = @"c:\sox-14-3-2\";
commandInfo.UseShellExecute = false;
commandInfo.RedirectStandardInput = true;
commandInfo.RedirectStandardOutput = true;
command.StartInfo = commandInfo;

command.Start();
command.StandardInput.WriteLine("set AUDIODEV=Line 2 (Virtual Audio Cable)");
command.StandardInput.WriteLine("rec.exe -d LyncRecieving.wav trim 0 0:20");


This code works fine but it runs in the same console window.
I want it to be in a separate window.

Can anybody help me with this?

Thanks.
Posted
Updated 7-Mar-11 21:14pm
v2
Comments
Sergey Alexandrovich Kryukov 8-Mar-11 3:10am    
Pretty interesting problem, my 5.
--SA
Dalek Dave 8-Mar-11 3:14am    
Edited for Readability.

You can add the commands for cmd.exe directly as arguments so you don't have to redirect standard in.
Example (not tested - just from memory):
commandInfo.Arguments = "/C /S \"\"set AUDIODEV=Line 2 (Virtual Audio Cable)&&rec.exe -d LyncRecieving.wav trim 0 0:20\"\"";

See cmd.exe /? for a complete reference und explanations.
 
Share this answer
 
Hi,

If your purpose is to display something in a second console window then create two console applications and use Piped Streams.

As an example..

In the main application

C#
    static void Main(string[] args)
    {

        Process command = new Process();
        ProcessStartInfo commandInfo = new ProcessStartInfo("ConsoleReceiver.exe");//ConsoleReceiver.exe is a second console application shown below
        commandInfo.UseShellExecute = true;
        command.StartInfo = commandInfo;
        command.Start();

        System.Threading.Thread.Sleep(2000);

        using (NamedPipeClientStream pipeClient =
new NamedPipeClientStream(".", "testpipe", PipeDirection.Out))
        {

            // Connect to the pipe or wait until the pipe is available.
            Console.Write("Attempting to connect to pipe...");
            pipeClient.Connect();

            Console.WriteLine("Connected to pipe.");
            Console.WriteLine("There are currently {0} pipe server instances open.",
               pipeClient.NumberOfServerInstances);
            try
            {
                // Read user input and send that to the client process.
                using (StreamWriter sw = new StreamWriter(pipeClient))
                {
                    sw.AutoFlush = true;
                    sw.WriteLine("Hi I am Server. Do you know me?");
                }
            }
            // Catch the IOException that is raised if the pipe is
            // broken or disconnected.
            catch (IOException ex)
            {
                Console.WriteLine("ERROR: {0}", ex.Message);
            }
        }

        Console.WriteLine("OK");


    }


In a second console application (after compile you may copy it to the bin dir of main application or use absoulte path of this in the main application)


C#
static void Main(string[] args)
{
    using (NamedPipeServerStream pipeServer =
        new NamedPipeServerStream("testpipe", PipeDirection.In))
    {
        Console.WriteLine("NamedPipeServerStream object created.");
        // Wait for a client to connect
        Console.Write("Waiting for client connection...");
        pipeServer.WaitForConnection();
        Console.WriteLine("Client connected.");
        using (StreamReader sr = new StreamReader(pipeServer))
        {
            // Display the read text to the console
            string temp;
            while ((temp = sr.ReadLine()) != null)
            {
                new StreamWriter(Console.OpenStandardOutput()).WriteLine(temp);
                Console.WriteLine(temp);
                Console.ReadLine();
            }
        }
    }



}


Hope this helps
 
Share this answer
 
v2
Comments
Albin Abel 8-Mar-11 8:18am    
Under the hood can execute cmd.exe (if to execute any command) with arguments from the second console. Also I am closing the streams, it is up to necessary till when to keep the streams open
This code will help you to open separate window of cmd
C#
using System.Diagnostics;
class Program
{
    static void Main(string[] args)
    {

        Console.WriteLine("going to run");
        Console.ReadLine();
        ProcessStartInfo pi = new ProcessStartInfo("cmd.exe");
        pi.CreateNoWindow = true;
        pi.WorkingDirectory = @"c:\Windows";

        Process p = new Process();
        p.StartInfo = pi;
        p.Start();
        Console.ReadLine();
    }
}
 
Share this answer
 
Comments
ledtech3 15-Oct-11 14:56pm    
I Have been trying to figure this simple bit out for quite a while. I used the base of your code and modified it for VB.net so I could, at a button click open a command window with the location listed in a text box.
Dim fullArg As String
Dim MgmgtclassPath As String
MgmgtclassPath = TXTboxMGMTLocation.Text
fullArg = (MgmgtclassPath.ToString)


Dim pst As New ProcessStartInfo("cmd.exe")
pst.CreateNoWindow = True
pst.WorkingDirectory = fullArg
Dim p As New Process()
p.StartInfo = pst
p.Start()
Reza Alipour Fard 3-Jan-13 17:00pm    
I test your code.
It's correct and perfect.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900