如何将一个字符串赋值给一个stream

linkejian 2006-03-03 10:01:44
如下
string str= "hello world" ;
MemoryStream myStream = new MemoryStream();
目标:
myStream = string;??
...全文
201 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
diandian82 2006-03-03
  • 打赏
  • 举报
回复
学会了!
hy98521 2006-03-03
  • 打赏
  • 举报
回复
Chocoboboy() ( )
同意
chentianfen 2006-03-03
  • 打赏
  • 举报
回复
学习
idda 2006-03-03
  • 打赏
  • 举报
回复
byte[] buffer = System.Text.Encoding.Unicode.GetBytes("faint");
MemoryStream stream = new MemoryStream(buffer);
Chocoboboy 2006-03-03
  • 打赏
  • 举报
回复
可以用stream的write或writeline方法写进去
達魔 2006-03-03
  • 打赏
  • 举报
回复
可以使用序列化功能:
string p_serialObj = "******..."; //你的字符串
MemoryStream memStream = new MemoryStream();
BinaryFormatter binFormatter = new BinaryFormatter();
binFormatter.Serialize(memStream, p_serialObj);
lovvver 2006-03-03
  • 打赏
  • 举报
回复
你传进的参数,查看是否为空。

程序里加了using System.Text;应该是可以的。
lovefootball 2006-03-03
  • 打赏
  • 举报
回复
MemoryStream ms = new MemoryStream(System.Text.Encoding.Default.GetBytes(str));
linkejian 2006-03-03
  • 打赏
  • 举报
回复
to lovvver(春晖)
出现错误
string引用没有设置为string的实例
lovvver 2006-03-03
  • 打赏
  • 举报
回复
byte[] bt = System.Text.Encoding.Default .GetBytes (str);
v192 2006-03-03
  • 打赏
  • 举报
回复
流是2进制的,也就是byte[],所以你需要把string类型通过Encoding类转换为byte[]再导入到流。

110,532

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧