如何从尚未关闭的进程中读取数据

本文关键字:进程 读取 数据 | 更新日期: 2023-09-27 18:01:25

我正在用c#编写一个简单的程序,允许客户端连接到服务器并在该服务器上运行MS DOS命令。

程序工作得很好,后来我决定我想要cmd.exe进程运行,而客户端连接到服务器,这就是我被困的地方。我想这么做,这样我就能运行像CD这样的命令并更改工作目录,以前这不会有任何效果,因为cmd.exe进程在命令运行后被关闭。

如果进程已经退出,我似乎只能从StandardOutputStandardError流中读取,是否有任何解决方法?

下面是我在程序中使用的一些代码:

*创建并返回cmd.exe进程*

private Process createDOS()
{
    try
    {
        // Create a ProcessStartInfo class
        ProcessStartInfo startInfo = new ProcessStartInfo("cmd","");
        // Set up the values
        startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        startInfo.UseShellExecute = false;
        startInfo.RedirectStandardOutput = true;
        startInfo.RedirectStandardError = true;
        startInfo.RedirectStandardInput = true;
        startInfo.CreateNoWindow = false;
        // Create a cmd.exe process
        Process process = new Process();
        // Apply the start info and run
        process.StartInfo = startInfo;
        process.Start();  
        return process;
    }
    catch (Exception)
    {
        return null;
    }
}

将响应格式化为字符串的方法:

private string findResponse(StreamReader err,StreamReader outp)
{
    string output = outp.ReadToEnd();
    string error = err.ReadToEnd();
    string ret = "";
    // Add the output to the return field if the process actually output data
    if (output.Length > 0)
    {
        ret = "OUTPUT : " + output;
    }
    // Then attempt to add data from the error stream
    if (error.Length > 0)
    {
        // If output was read, add a newline character separating the two fields
        if (ret.Length > 0) ret = ret + Environment.NewLine;
        ret = ret + "ERROR : " + error;
    }
    // If there's no output, that means there's no error, which means the execution was silently succesful
    if (ret.Length <= 0)
    {
        ret = "Command execution succesful!";
    }
    return ret;
}

服务器监听块:

private void run()
{
    while (true)
    {
        Stream stream = null;
        StreamReader sr = null;
        StreamWriter sw = null;
        Socket socket = null;
        Process proc = null;
        try
        {
            socket = server.AcceptSocket();
            stream = new NetworkStream(socket);
            sr = new StreamReader(stream);
            sw = new StreamWriter(stream);
            String mode = sr.ReadLine();
            sw.WriteLine("Serverside link connected");
            sw.Flush();
            // Create cmd process in WINPROCESS mode
            if (mode == "WINPROCESS")
            {
                proc = createDOS();
            }
            String line;
            while ((line = sr.ReadLine()) != null)
            {
                if (mode == "WINPROCESS")
                {
                    proc.StandardInput.WriteLine(line);
                    proc.StandardInput.Flush();
                    // Response
                    sw.WriteLine(findResponse(proc.StandardError, proc.StandardOutput));
                    sw.Flush();
                }
                else
                {
                    sw.WriteLine(exeDOS(line));
                    sw.Flush();
                }
            }
        }
        catch (Exception ex)
        {
            // Silence
        }
        finally
        {
            if (socket != null) socket.Close();
            if (stream != null) stream.Close();
            if (sw != null) sw.Close();
            if (sr != null) sr.Close();
            if (proc != null) proc.Close();
        }
    }
}

任何帮助将不胜感激,谢谢!

如何从尚未关闭的进程中读取数据

您需要为OutputDataReceived和ErrorDataReceived事件添加处理程序:

    process.StartInfo.RedirectStandardOutput = true;
    process.OutputDataReceived += 
        new DataReceivedEventHandler(process_OutputDataReceived);
    process.StartInfo.RedirectStandardError = true;
    process.ErrorDataReceived += 
        new DataReceivedEventHandler(process_ErrorDataReceived);
    if (process.Start())
    {
        process.BeginOutputReadLine();
        process.BeginErrorReadLine();
        process.WaitForExit();
    }
}
void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
    Console.WriteLine(e.Data);
}
void process_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
    Console.WriteLine(e.Data);
}