程序中使用的cmd.exe从C# [英] Programmatic use of cmd.exe from C#

查看:119
本文介绍了程序中使用的cmd.exe从C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 我想从C#CMD.EXE运行一系列的命令。

  2. 我需要打开只有一个CMD
  3. $窗口b $ b
  4. 我需要保持cmd窗口通过执行和完成后打开。

  5. 我需要显示的命令在打开cmd窗口 [/编辑] 执行以及命令的输出。



所以基本上我想打开并使用CMD.EXE就像一个手动用户会。
我尝试了一些方法,但没有人能做到上述所有4项。



下面的代码的作品,但不显示命令/输出,完成后终止。 ?
任何帮助。



 进程p =新工艺(); 
的ProcessStartInfo信息=新的ProcessStartInfo(cmd.exe的);
info.RedirectStandardInput = TRUE;
info.UseShellExecute = FALSE;
info.CreateNoWindow = FALSE;
info.Arguments =/ K;

p.StartInfo =信息;
p.Start();使用

(StreamWriter的SW = p.StandardInput)
{
如果(sw.BaseStream.CanWrite)
{
sw.WriteLine(目录 );
sw.WriteLine(IPCONFIG);
}
}


解决方案

RedirectStandard ... -properties具有误导性。只要你把其中的一个,所有的三流将被重定向。这是因为底层的Windows API只有一个标志,用于控制重定向 - STARTF_USESTDHANDLES



由于你没' t将 RedirectStandardOutput 属性设置为true,标准输出流并不提供给你的代码,而是将被重定向到 Console.Out 自己的过程流。
这样,你的代码只要父进程是控制台应用程序工作正常;但是在Windows应用程序中,输出被重定向到虚无



这是简单的解决办法是暂时把你的父进程到一个控制台应用程序:

 函数[DllImport(KERNEL32.DLL)] 
[返回:的MarshalAs(UnmanagedType.Bool)
静态外部布尔AllocConsole( );

函数[DllImport(KERNEL32.DLL)]
[返回:的MarshalAs(UnmanagedType.Bool)
静态外部布尔FreeConsole();

静态无效的主要(字串[] args)
{
进程p =新工艺();
的ProcessStartInfo信息=新的ProcessStartInfo(cmd.exe的);
info.RedirectStandardInput = TRUE;
info.UseShellExecute = FALSE;
info.CreateNoWindow = FALSE;

p.StartInfo =信息;
AllocConsole();
p.Start();
FreeConsole();使用


(StreamWriter的SW = p.StandardInput)
{
如果(sw.BaseStream.CanWrite)
{
sw.WriteLine (目录);
sw.WriteLine(IPCONFIG);
}
}
}



对于保持控制台打开:让父进程一直围绕即使CMD.EXE退出后控制台,你可以只删除 FreeConsole()电话。然而,这可能是一个问题,如果你需要多个控制台,因为你的父进程不能在同一时间多个控制台关联。



另外,不关闭输入流,这样的cmd.exe保持运行。


  1. I want to run a series of commands on cmd.exe from C#.
  2. I need to open only one window of cmd
  3. I need to keep the cmd window open through the execution and after the completion.
  4. I need to display the commands [edit]in the opened cmd window[/edit] executed as well as the output of the commands.

So basically I want to open and use the cmd.exe just like a manual user would. I tried some methods, but none could do all 4 items above.

Below code works but does not display the commands/outputs and terminates after completion. Any help?

Process p = new Process();
ProcessStartInfo info = new ProcessStartInfo("cmd.exe");
info.RedirectStandardInput = true;
info.UseShellExecute = false;
info.CreateNoWindow = false;
info.Arguments = "/k";

p.StartInfo = info;
p.Start();

using (StreamWriter sw = p.StandardInput)
{
    if (sw.BaseStream.CanWrite)
    {
         sw.WriteLine("dir");
         sw.WriteLine("ipconfig");
    }
}

解决方案

The RedirectStandard...-properties are misleading. As soon as you set one of them, all three streams will be redirected. This is because the underlying Windows API only has a single flag to control redirection - STARTF_USESTDHANDLES.

Because you didn't set the RedirectStandardOutput property to true, the stdout streams isn't made available to your code, but instead will be redirected to the Console.Out stream of your own process. Thus, your code works fine as long as the parent process is a console application; but in a Windows application, the output gets redirected into nothingness.

An easy workaround is to temporarily turn your parent process into a console application:

[DllImport("kernel32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool AllocConsole();

[DllImport("kernel32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool FreeConsole();

static void Main(string[] args)
{
    Process p = new Process();
    ProcessStartInfo info = new ProcessStartInfo("cmd.exe");
    info.RedirectStandardInput = true;
    info.UseShellExecute = false;
    info.CreateNoWindow = false;

    p.StartInfo = info;
    AllocConsole();
    p.Start();
    FreeConsole();


    using (StreamWriter sw = p.StandardInput)
    {
        if (sw.BaseStream.CanWrite)
        {
             sw.WriteLine("dir");
             sw.WriteLine("ipconfig");
        }
    }
}

As for keeping the console open: you could just remove the FreeConsole() call so that the parent process keeps the console around even after cmd.exe exits. However this may be a problem if you need multiple consoles, as your parent process can't be associated with more than one console at a time.

Alternatively, don't close the input stream so that cmd.exe keeps running.

这篇关于程序中使用的cmd.exe从C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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