Skip to main content
1 of 3
Robocide
  • 6.8k
  • 5
  • 41
  • 43

great answers i used mix of answers like this :

public static Stream ToStream(this string str, Encoding enc = null) { enc = enc ?? Encoding.UTF8; return new MemoryStream(enc.GetBytes(str ?? "")); } 

and then i use it like this :

String someStr="This is a Test"; Encoding enc = getEncodingFromSomeWhere(); using (Stream stream = someStr.ToStream(enc)) { //do something with stream.... } 
Robocide
  • 6.8k
  • 5
  • 41
  • 43