public static Stream ToStream(this string str, Encoding enc = null){enc = enc ?? Encoding.UTF8;return new MemoryStream(enc.GetBytes(str ?? ""));}
然后我这样使用它:
String someStr="This is a Test";Encoding enc = getEncodingFromSomeWhere();using (Stream stream = someStr.ToStream(enc)){// Do something with the stream....}