获取StreamReader的长度 [英] Get length of Streamreader

查看:396
本文介绍了获取StreamReader的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能得到的长度的StreamReader ,因为我知道什么都不会再被写入其中。
我想,也许我可以将所有数据传递给的MemoryStream ,其中有一个名为方法长度 ,但我被困在如何一个byte []追加到的MemoryStream

How can I get the length of a StreamReader, as I know nothing will be written to it anymore. I thought that maybe I could pass all the data to a MemoryStream, which has a method called Length, but I got stuck on how to append a byte[] to 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();
        }



所以,我怎么能dinamically追加到一个MemoryStream一个byte []?
还有什么比这更好的让输出的长度这样我就可以发出警告,将要发送的消息的大小的另一端?

So, how can I dinamically append a byte[] to a MemoryStream? There is any way better than this to get the length of output so I can warn the other end about the size of the message which will be sent?

推荐答案

的MemoryStream 拥有的 MemoryStream.Write方法,该写入字节数组流:

MemoryStream has MemoryStream.Write Method , which writes a byte array to the stream:

ms.Write(bytebuffer,0,bytebuffer.Length);



所以,你可以调用它字节的另一部分添加到输出流。但是请记住,要能后,所有的写操作都在从的MemoryStream 阅读,你将不得不使用的 MemoryStream.Seek方法设置当前流回到开头中的位置:

So, you can call it to add another portion of bytes to the output stream. However remember, to be able to read from MemoryStream after all write operations are over, you'll have to use MemoryStream.Seek Method to set the position within the current stream to its beginning:

//all write operations
ms.Seek(0, SeekOrigin.Begin);
//now ready to be read

这是最简单的方法。 cousre,你可以动态地横跨流动,而读/写。但是,这可能是容易出错。

This is the easiest approach. Of cousre, you may dynamically move across the stream, while reading/writing. But that may be error prone.

要获得流,即 ms.Length ,你不的长度'T不得不寻求流的开头。

To get the length of the stream, i.e. ms.Length, you don't have to seek the begining of the stream.

我想,这是值得大家注意的是,如果的ByteBuffer 只有将它们复制到的MemoryStream ,你可以使用的 MemoryStream.WriteByte 的方法代替。这将让你取消的ByteBuffer 的。

I guess, it's worth to note, that if bytebuffer is used only to store bytes before copying them into the MemoryStream, you could use MemoryStream.WriteByte Method instead. This would let you abolish bytebuffer at all.

for (int i = 0; i < length; i++)
{
    ms.WriteByte(Convert.ToByte(charbuffer[i]));
}

这篇关于获取StreamReader的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆