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
Calling Async Method on Button Click
How to Check If a User Belongs to an Ad Group
Button in a Column, Getting the Row from Which It Came on the Click Event Handler
Why Is a Round-Trip Conversion via a String Not Safe for a Double
Setting a Webrequest's Body Data
How to Use Transactionscope in C#
How to Override with Derived Types
Cannot Resolve Scoped Service from Root Provider .Net Core 2
Peculiar Overload Resolution with While (True)
Dynamic Button Creation & Placing Them in a Predefined Order Using C#
Sendinput Doesn't Perform Click Mouse Button Unless I Move Cursor
Sequence Contains No Matching Element
How to Delete a File After Checking Whether It Exists
Run the Current Application as Single Instance and Show the Previous Instance
Ways to Synchronize Interface and Implementation Comments in C#