创建命令提示符

时间:2015-06-11 16:42:01

标签: c# wpf process stream

我想在WPF中创建一个命令提示符,用户可以在同一窗口中键入命令并获取输出,这与原始命令提示符的工作原理完全相同。

最初我使用Process.Start()并将其放入另一个线程中以阻止主线程。我使用StreamReader和StreamWriter来编写和捕获输出。但是我无法持续监听输出或写命令。

这是我的代码,直到现在 -

public class TerminalController
{
    public Process CmdProcess { get; set; }
    public StreamReader Reader { get; set; }
    public StreamWriter Writer { get; set; }
    public void Init()
    {
        if (CmdProcess != null)
        {
            try
            {
                Close();
            }
            catch (Exception ex)
            {
                Logger.LogException(ex);
            }
        }
        CmdProcess = new Process();
        System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
        startInfo.FileName = "cmd";
        startInfo.WindowStyle = ProcessWindowStyle.Normal;
        startInfo.CreateNoWindow = true;
        startInfo.RedirectStandardInput = true;
        startInfo.RedirectStandardOutput = true;
        startInfo.UseShellExecute = false;
        CmdProcess.StartInfo = startInfo;
        CmdProcess.Start();
        Writer = CmdProcess.StandardInput;

        StartReading();
    }
    public void Init(string command)
    {
        if (CmdProcess != null)
        {
            try
            {
                Close();
            }
            catch (Exception ex)
            {
                Logger.LogException(ex);
            }
        }
        CmdProcess = new Process();
        System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
        startInfo.FileName = "cmd";
        startInfo.WindowStyle = ProcessWindowStyle.Normal;
        startInfo.CreateNoWindow = true;
        startInfo.RedirectStandardInput = true;
        startInfo.RedirectStandardOutput = true;
        startInfo.UseShellExecute = false;
        CmdProcess.StartInfo = startInfo;
        CmdProcess.Start();

    }

    private void Write(String command)
    {
        using (Writer = CmdProcess.StandardInput)
        {
            Writer.Write(command);
        }
    }

    private void StartReading()
    {
        using (Reader = CmdProcess.StandardOutput)
        {
            RaiseTerminalReadEvent(null, Reader.ReadToEnd());
        }
    }
    public async void RunCommand(String command)
    {
        RaiseTerminalWriteEvent(command);
        await Task.Run(() => Write(command));
        await Task.Run(() => StartReading());
        //Close();
    }

    public Task InitAsync()
    {
        return Task.Run(() => Init());
    }

    public void Close()
    {
        try
        {
            CmdProcess.Close();
            Reader.Close();
            Writer.Close();
            CmdProcess = null;
        }
        catch (Exception ex)
        {
            Logger.LogException(ex);
        }
    }

    internal Task RunCommandAsync(string command)
    {
        return Task.Run(() => RunCommand(command));
    }
}

我只能运行一次命令,这是我发送dir命令时的输出 -

dir
Microsoft Windows [Version 6.3.9600]
(c) 2013 Microsoft Corporation. All rights reserved.

E:\Microsoft\WPF\Example\Example\bin\Debug>More? 

如果我再次发送命令,则会出现以下异常 -

Cannot write to a closed TextWriter.
at System.IO.__Error.WriterClosed()
   at System.IO.StreamWriter.Flush(Boolean flushStream, Boolean flushEncoder)
   at System.IO.StreamWriter.Write(String value)
   at Example.Controllers.TerminalController.Write(String command) in e:\Microsoft\WPF\Example\Example\Controllers\TerminalController.cs:line 91

那么我怎样才能连续运行命令并监听输出。我是否需要每次都创建一个新流程?还有其他选择吗?

1 个答案:

答案 0 :(得分:1)

您的using块会在您第一次使用它们时立即处理它们。

因此,您无法再次使用它们。

不要这样做。