Convert String to System.Io.Stream

Convert String to System.IO.Stream

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();

Converting string to System.IO.Stream

streamwriter constructor parameters wrong ..

        string fileName = "your file path";
using(FileStream fileStream = new FileStream(fileName, FileMode.OpenOrCreate))
{
using (StreamWriter sw = new StreamWriter(fileStream, Encoding.UTF8))
{
MySerializableClass msc = new MySerializableClass();
XmlSerializer serializer = new XmlSerializer(typeof(MySerializableClass));
serializer.Serialize(sw, msc);
}
}

if you want to without FileStream, use this constructor.. StreamWriter(string path, bool append, Encoding encoding)

        string fileName = "your file path";
using (StreamWriter sw = new StreamWriter(fileName, true, Encoding.UTF8))
{
MySerializableClass msc = new MySerializableClass();
XmlSerializer serializer = new XmlSerializer(typeof(MySerializableClass));
serializer.Serialize(sw, msc);
}

How do I generate a stream from a string?

public static Stream GenerateStreamFromString(string s)
{
var stream = new MemoryStream();
var writer = new StreamWriter(stream);
writer.Write(s);
writer.Flush();
stream.Position = 0;
return stream;
}

Don't forget to use Using:

using (var stream = GenerateStreamFromString("a,b \n c,d"))
{
// ... Do stuff to stream
}

About the StreamWriter not being disposed. StreamWriter is just a wrapper around the base stream, and doesn't use any resources that need to be disposed. The Dispose method will close the underlying Stream that StreamWriter is writing to. In this case that is the MemoryStream we want to return.

In .NET 4.5 there is now an overload for StreamWriter that keeps the underlying stream open after the writer is disposed of, but this code does the same thing and works with other versions of .NET too.

See Is there any way to close a StreamWriter without closing its BaseStream?

cannot convert from 'string' to 'System.IO.Stream' while reading a file line by line using StreamReader

If you want to read a file the easiest way is

var path = "c:\\mypath\\to\\my\\file.txt";
var lines = File.ReadAllLines(path);

foreach (var line in lines)
{
Console.WriteLine(line);
}

You can also do it like this:

var path = "c:\\mypath\\to\\my\\file.txt";
using (var reader = new StreamReader(path))
{
while (!reader.EndOfStream)
{
Console.WriteLine(reader.ReadLine());
}
}

Cannot convert string to system.io

FilePath.Text returns a string which is the location of the file on the drive

the below code would work

using (StreamReader reader = new StreamReader(FilePath.Text))
{
// We read the file then we split it.
string lines = reader.ReadToEnd();
string[] splittedArray = lines.Split(',');

// We will check here if any of the strings is empty (or just whitespace).
foreach (string currentString in splittedArray)
{
if (currentString.Trim() != "")
{
// If the string is not empty then we add to our temporary list.
temp.Add(currentString);
}
}

// We have our splitted strings in temp List.
// If you need an array instead of List, you can use ToArray().
finalArray = temp.ToArray();
}

Converting String to Stream

You can use the following piece of code....

byte[] bytes = Convert.FromBase64String(stringinput);
MemoryStream stream = new MemoryStream(bytes);
IInputStream is=stream.AsRandomAccessStream(); //It will return an IInputStream object


Related Topics



Leave a reply



Submit