C# 在控制台中获得问号

C# 在控制台中获得问号,c#,.net,named-pipes,C#,.net,Named Pipes,使用此示例:创建IPC(服务器/客户机)以将消息从一个进程传递到另一个进程 它在使用Windows窗体时非常有效,但在将消息传递到控制台时,它会附加一堆消息?????????????????????在每条消息的末尾 // This works and outputs the correct message. PipeMessage.Invoke(stringData); txtMessage.Text = message; // Outputting same message to conso

使用此示例:创建IPC(服务器/客户机)以将消息从一个进程传递到另一个进程

它在使用Windows窗体时非常有效,但在将消息传递到控制台时,它会附加一堆消息?????????????????????在每条消息的末尾

// This works and outputs the correct message.
PipeMessage.Invoke(stringData);
txtMessage.Text = message;

// Outputting same message to console appends bunch of question marks
Console.WriteLine(stringData);
我需要看到原始消息干净,没有任何问号。请帮忙

namespace PipesServerTest
{
    // Delegate for passing received message back to caller
    public delegate void DelegateMessage(string Reply);

    class PipeServer
    {
        public event DelegateMessage PipeMessage;
        string _pipeName;

        public void Listen(string PipeName)
        {
            try
            {
                // Set to class level var so we can re-use in the async callback method
                _pipeName = PipeName;
                // Create the new async pipe 
                NamedPipeServerStream pipeServer = new NamedPipeServerStream(PipeName, PipeDirection.In, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);

                // Wait for a connection
                pipeServer.BeginWaitForConnection(new AsyncCallback(WaitForConnectionCallBack), pipeServer);
            }
            catch (Exception oEX)
            {
                Debug.WriteLine(oEX.Message);
            }
        }

        private void WaitForConnectionCallBack(IAsyncResult iar)
        {
            try
            {
                // Get the pipe
                NamedPipeServerStream pipeServer = (NamedPipeServerStream)iar.AsyncState;
                // End waiting for the connection
                pipeServer.EndWaitForConnection(iar);

                byte[] buffer = new byte[255];

                // Read the incoming message
                pipeServer.Read(buffer, 0, 255);

                // Convert byte buffer to string
                string stringData = Encoding.UTF8.GetString(buffer, 0, buffer.Length);

                // Pass message back to calling form
                PipeMessage.Invoke(stringData);

                // Kill original sever and create new wait server
                pipeServer.Close();
                pipeServer = null;
                pipeServer = new NamedPipeServerStream(_pipeName, PipeDirection.In, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);

                // Recursively wait for the connection again and again....
                pipeServer.BeginWaitForConnection(new AsyncCallback(WaitForConnectionCallBack), pipeServer);
            }
            catch
            {
                return;
            }
        }
    }
}
表格:


看起来您的控制台字符集编码设置为ASCII。 使用以下属性将编码设置为UTF-8:

Console.OutputEncoding = System.Text.Encoding.UTF8;

感谢@500-内部服务器错误

替换此项:

pipeServer.Read(buffer, 0, 255);
string stringData = Encoding.UTF8.GetString(buffer, 0, buffer.Length);
为此:

var bytesRead = pipeServer.Read(buffer, 0, 255);
string stringData = Encoding.UTF8.GetString(buffer, 0, bytesRead);

NamedPipeServerStream.Read(…)
之后,您应该使用返回值(当前未捕获)进行进一步处理,而不是使用缓冲区的大小。@500 InternalServerError我想下一行会处理
string stringData=Encoding.UTF8.GetString(buffer,0,buffer.Length)
No,
buffer.Length
是您在声明
buffer
(255)时给出的大小,但是
Read()
可能无法从源代码中获得那么多-它返回它实际得到的结果。我不会发送超过
255
,因此这是正常的。请提供一个“您应该使用返回值(当前未捕获)进行进一步处理”的示例。类似于
var bytesRead=pipeServer.Read(缓冲区,0,255)stringData=Encoding.UTF8.GetString(缓冲区,0,字节读取)。这没有什么区别。即使使用
if(stringData==“testmessage-1”){Console.WriteLine(“获取第一条消息”);}
以编程方式进行检查也不起作用,之后应用了一些垃圾。您可以发布屏幕截图吗?tnxSorry与字符集无关,这都是
NamedPipeServerStream.Read(…)
问题
var bytesRead = pipeServer.Read(buffer, 0, 255);
string stringData = Encoding.UTF8.GetString(buffer, 0, bytesRead);