Convert string to filestream in c#
Solution 1
First of all change your method to allow Stream
instead of FileStream
. FileStream
is an implementation which, as I remember, does not add any methods or properties, just implement abstract class Stream
. And then using below code you can convert string
to Stream
:
public Stream GenerateStreamFromString(string s)
{
MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter(stream);
writer.Write(s);
writer.Flush();
stream.Position = 0;
return stream;
}
Solution 2
As FileStream class provides a stream for a file and hence it's constructor requires the path of the file,mode, permission parameter etc. to read the file into stream and hence it is used to read the text from file into stream. If we need to convert string to stream first we need to convert string to bytes array as stream is a sequence of bytes. Below is the code.
//Stream is a base class it holds the reference of MemoryStream
Stream stream = new MemoryStream();
String strText = "This is a String that needs to beconvert in stream";
byte[] byteArray = Encoding.UTF8.GetBytes(strText);
stream.Write(byteArray, 0, byteArray.Length);
//set the position at the beginning.
stream.Position = 0;
using (StreamReader sr = new StreamReader(stream))
{
string strData;
while ((strData= sr.ReadLine()) != null)
{
Console.WriteLine(strData);
}
}
now he who must not be named.
#SOreadytohelp There is this joy in helping others. Thanks SO
Updated on July 09, 2022Comments
-
now he who must not be named. almost 2 years
Just started with writing unit tests and I am now, blocked with this situation:
I have a method which has a FileStream object and I am trying to pass a "string" to it. So, I would like to convert my string to FileStream and I am doing this:
File.WriteAllText(string.Concat(Environment.ExpandEnvironmentVariables("%temp%"), @"/test.txt"), testFileContent); //writes my string to a temp file! new FileStream(string.Concat(Environment.ExpandEnvironmentVariables("%temp%"), @"/test.txt"), FileMode.Open) //open that temp file and uses it as a fileStream!
close the file then!
But, I guess there must be some very simple alternative to convert a string to a fileStream.
Suggestions are welcome! [Note there are other answers to this question in stackoverflow but none seems to be a straight forward solution to that]
Thanks in advance!
-
now he who must not be named. almost 11 yearsThe problem is that I cannot change the code. It's a library which is already compiled and I don't have a source code!
-
Piotr Stapp almost 11 yearsSo override
FileStream
with your own class. -
lloydom almost 11 yearsyou will have to mock the filestream then as mentioned by others
-
Vilius Surblys almost 11 years@nowhewhomustnotbenamed. You said this was for unit testing; why are you unit testing a method that you cannot change?
-
Alan Baljeu over 5 years@RowlandShaw to be sure the method someone else made works as expected.
-
Vilius Surblys over 5 years@AlanBaljeu Whilst a useful thing to do for acceptance testing, that doesn't make it a good candidate for unit testing
-
Alan Baljeu over 5 yearsI don't distinguish those categories. It's just tests.