转换字符串的System.IO.Stream [英] Convert String to System.IO.Stream

查看:155
本文介绍了转换字符串的System.IO.Stream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要转换为字符串类型的System.IO.Stream传递给另一个方法。

我试过这个不成功。

 流流=新StringReader(内容);


解决方案

试试这个:

  //字符串转换为流
字节[]的字节数组= Encoding.UTF8.GetBytes(内容);
//字节[]的字节数组= Encoding.ASCII.GetBytes(内容);
MemoryStream的流=新的MemoryStream(字节阵列);

  //流转换为字符串
StreamReader的读者=新的StreamReader(流);
字符串文本= reader.ReadToEnd();

I need to convert a String to System.IO.Stream type to pass to another method.

I tried this unsuccessfully.

Stream stream = new StringReader(contents);

解决方案

Try this:

// convert string to stream
byte[] byteArray = Encoding.UTF8.GetBytes(contents);
//byte[] byteArray = Encoding.ASCII.GetBytes(contents);
MemoryStream stream = new MemoryStream(byteArray);

and

// convert stream to string
StreamReader reader = new StreamReader(stream);
string text = reader.ReadToEnd();

这篇关于转换字符串的System.IO.Stream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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