json - Usage of StreamReader in C# -
i want read file data.json , convert string.
my code one:
string json = null; using (streamreader sr = new streamreader("data.json")) { json = sr.readtoend(); }
but visual studio tells me streamreader
not expect string constructor argument.
how can tell streamreader want read file data.json?
actually streamreader
supports constructor accepts file path platforms, not all. anyway - use file.readalltext
:
string json = file.readalltext("data.json");
it creates streamreader
internally (link source):
using (var sr = new streamreader(path, encoding)) return sr.readtoend();
update: can pass stream streamreader
. use filestream
open stream reading file, , pass streamreader
:
string json = null; using (var stream = new filestream("data.json", filemode.open)) using (var reader = new streamreader(stream)) json = reader.readtoend();
Comments
Post a Comment