将数据写入MemoryStream [英] Writing data into MemoryStream

查看:107
本文介绍了将数据写入MemoryStream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

byte[] storage = new byte[3000000];
Stream fileStream = Stream.Null;
MemoryStream memoryStream = new MemoryStream(storage);
TextWriter streamWriter = new StreamWriter(memoryStream);
streamWriter.WriteLine("Companies Information");

// Writing Data into the File...
List<CompanySearchResultItem> list = SearchCompany(businessEntitySearchArgs);
// iterate to the list and print the remaining rows.
foreach (CompanySearchResultItem item in list)
{
   streamWriter.WriteLine(item.CompanyName);
}

streamWriter.Flush();

byte[] CompanyData = new byte[memoryStream.Length];
fileStream.Write(CompanyData, 0, (int)memoryStream.Length);


这是我的代码.我需要CompanyData中的数据,但无法获取它.请帮助我.


This is my code. I need data in CompanyData but I was not able to get it. Please help me.

推荐答案

使用此功能可以解决您的问题,


Use this that may solve your problem,


byte[] CompanyData = new byte[uniEncoding.GetByteCount(memoryStream)];
fileStream.Write(uniEncoding.GetBytes(CompanyData),
        0, uniEncoding.GetByteCount(memoryStream));



希望对您有所帮助.

如果有帮助,请 投票 接受答案 .



Hope it helps.

Please vote and Accept Answer if it Helped.


为什么你是那样做的吗?为什么不创建/打开文本文件并写下名称呢?那将不超过五到六行代码,并且还可以轻松维护.
Why are you doing it that way? Why don''t you just create/open a text file, and write the names? That would be no more than five or six lines of code, and easiser to maintain as well.


如果您想要数组CompanyData中的数据,为什么不只是使用memoryStream.ToArray()?
或至少使用Read代替Write.
If you want the data in the array CompanyData, why don''t you just use memoryStream.ToArray()?
Or at least use Read instead of Write..


这篇关于将数据写入MemoryStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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