C# 如何从尚未';你没关门吗?

C# 如何从尚未';你没关门吗?,c#,.net,process,networkstream,C#,.net,Process,Networkstream,我正在用C语言编写一个简单的程序,允许客户端连接到服务器并在该服务器上运行MS DOS命令 这个程序运行得很好,后来我决定在客户端连接到服务器时运行cmd.exe进程,这就是我所要解决的问题。我想要这个,这样我就可以 运行CD之类的命令并更改工作目录,以前这不会有任何效果,因为在命令运行后cmd.exe进程已关闭 如果进程已退出,我似乎只能从StandardOutput和StandardError流中读取,是否有解决方法 以下是我在程序中使用的一些代码: *创建并返回cmd.exe进程:* 将响

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

这个程序运行得很好,后来我决定在客户端连接到服务器时运行cmd.exe进程,这就是我所要解决的问题。我想要这个,这样我就可以 运行CD之类的命令并更改工作目录,以前这不会有任何效果,因为在命令运行后cmd.exe进程已关闭

如果进程已退出,我似乎只能从
StandardOutput
StandardError
流中读取,是否有解决方法

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

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

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

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

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

您需要向和事件添加处理程序:

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