Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 .
 
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 7-Mar-11 20:28pm
Edited 7-Mar-11 21:14pm
Dalek Dave426.3K
v2
Comments
SAKryukov at 8-Mar-11 3:10am
   
Pretty interesting problem, my 5. --SA
Dalek Dave at 8-Mar-11 3:14am
   
Edited for Readability.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
 
        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)
 

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
  Permalink  
v2
Comments
AlbinAbel at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

This code will help you to open separate window of cmd
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();
    }
}
  Permalink  
Comments
ledtech3 at 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 at 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)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 9,225
1 OriginalGriff 5,395
2 Peter Leow 4,100
3 Maciej Los 3,540
4 Abhinav S 3,333


Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 16 May 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid