Click here to Skip to main content
11,702,929 members (64,986 online)
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 Dave433.2K
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
0 Maciej Los 415
1 Sergey Alexandrovich Kryukov 325
2 OriginalGriff 260
3 CHill60 120
4 Andy Lanng 119
0 OriginalGriff 8,379
1 Sergey Alexandrovich Kryukov 7,587
2 CPallini 4,947
3 Maciej Los 4,501
4 Mika Wendelius 3,566


Advertise | Privacy | Mobile
Web01 | 2.8.150819.1 | Last Updated 23 Sep 2014
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100