在不同窗口的控制台应用程序中启动cmd.exe。 [英] Starting cmd.exe in console application in different window.

查看:63
本文介绍了在不同窗口的控制台应用程序中启动cmd.exe。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我想在一个单独的窗口中从控制台应用程序打开一个命令提示符,并在新的命令提示符下执行一些命令行。



我可以通过设置UseShellExecute = false打开一个单独的窗口,但无法写入任何内容。

StandardIn尚未重定向。

我收到此异常。



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.

推荐答案

您可以直接将cmd.exe的命令添加为参数,这样您就不必重定向标准。

示例(未测试 - 仅来自内存):

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\"\"";



参见 cmd.exe /?获取完整的参考和解释。


See cmd.exe /? for a complete reference und explanations.






如果您的目的是在第二个控制台窗口中显示内容,则创建两个控制台应用程序并使用Piped Streams。



作为示例..



在主应用程序中



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


    }





在第二个控制台应用程序中(编译后你可以将其复制到主应用程序的bin目录或在主应用程序中使用absoulte路径)







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


此代码将帮助您打开cmd的单独窗口

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();
    }
}


这篇关于在不同窗口的控制台应用程序中启动cmd.exe。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆