在控制台中获取问号
Getting question marks in console
使用此示例:https://www.codeproject.com/Tips/492231/Csharp-Async-Named-Pipes 创建 IPC (Server/Client) 以将消息从一个进程传递到另一个进程。
它在使用 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;
}
}
}
}
表格:
namespace PipesServerTest
{
public partial class Form1 : Form
{
public delegate void NewMessageDelegate(string NewMessage);
private PipeServer _pipeServer;
public Form1()
{
InitializeComponent();
_pipeServer = new PipeServer();
_pipeServer.PipeMessage += new DelegateMessage(PipesMessageHandler);
}
private void cmdListen_Click(object sender, EventArgs e)
{
try
{
_pipeServer.Listen("TestPipe");
txtMessage.Text = "Listening - OK";
cmdListen.Enabled = false;
}
catch (Exception)
{
txtMessage.Text = "Error Listening";
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void PipesMessageHandler(string message)
{
try
{
if (this.InvokeRequired)
{
this.Invoke(new NewMessageDelegate(PipesMessageHandler), message);
}
else
{
txtMessage.Text = message;
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
_pipeServer.PipeMessage -= new DelegateMessage(PipesMessageHandler);
_pipeServer = null;
}
}
}
您的控制台字符集编码似乎设置为 ASCII。
使用 OutputEncoding 属性:
将编码设置为 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);
使用此示例:https://www.codeproject.com/Tips/492231/Csharp-Async-Named-Pipes 创建 IPC (Server/Client) 以将消息从一个进程传递到另一个进程。
它在使用 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;
}
}
}
}
表格:
namespace PipesServerTest
{
public partial class Form1 : Form
{
public delegate void NewMessageDelegate(string NewMessage);
private PipeServer _pipeServer;
public Form1()
{
InitializeComponent();
_pipeServer = new PipeServer();
_pipeServer.PipeMessage += new DelegateMessage(PipesMessageHandler);
}
private void cmdListen_Click(object sender, EventArgs e)
{
try
{
_pipeServer.Listen("TestPipe");
txtMessage.Text = "Listening - OK";
cmdListen.Enabled = false;
}
catch (Exception)
{
txtMessage.Text = "Error Listening";
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void PipesMessageHandler(string message)
{
try
{
if (this.InvokeRequired)
{
this.Invoke(new NewMessageDelegate(PipesMessageHandler), message);
}
else
{
txtMessage.Text = message;
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
_pipeServer.PipeMessage -= new DelegateMessage(PipesMessageHandler);
_pipeServer = null;
}
}
}
您的控制台字符集编码似乎设置为 ASCII。 使用 OutputEncoding 属性:
将编码设置为 UTF-8Console.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);