我怎样才能得到 a 的长度StreamReader
,因为我知道不会再写任何东西了。我想也许我可以将所有数据传递给 a MemoryStream
,它有一个名为 的方法Length
,但我被困在如何将 byte[] 附加到 a 上MemoryStream
。
private void Cmd(string command, string parameter, object stream)
{
StreamWriter writer = (StreamWriter)stream;
StreamWriter input;
StreamReader output;
Process process = new Process();
try
{
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.FileName = "cmd";
process.Start();
input = process.StandardInput;
output = process.StandardOutput;
input.WriteLine(command + " " + parameter);
input.WriteLine("exit");
using (MemoryStream ms = new MemoryStream())
{
int length = 1024;
char[] charbuffer = new char[length];
byte[] bytebuffer = new byte[length];
while (!output.EndOfStream)
{
output.Read(charbuffer, 0, charbuffer.Length);
for (int i = 0; i < length; i++)
{
bytebuffer[i] = Convert.ToByte(charbuffer[i]);
}
//append bytebuffer to memory stream here
}
long size = ms.Length;
writer.WriteLine(size);
writer.Flush(); //send size of the following message
//send message
}
}
catch (Exception e)
{
InsertLog(2, "Could not run CMD command");
writer.WriteLine("Not valid. Ex: " + e.Message);
}
writer.Flush();
}
那么,我怎样才能动态地将一个字节 [] 附加到一个 MemoryStream 呢?有什么比这更好的方法来获得长度,output
所以我可以警告另一端关于将要发送的消息的大小?