17 Replies
I assume it has something to do with streamreader?
Sure, there are multiple ways to read a file
Usually it comes down to either reading the entire file into memory at once, or reading it as a stream so it doesn't fill up the RAM if you're reading something that's 8 gigs
If I’m reading a simple text file, I assume I don’t need streamreader since it’s a tiny textfile?
Yeah
Just
var text = await File.ReadAllTextAsync("file.txt")
will do
Or even the synchronous version if needs mustIt says ‘await’ operator can only be used when contained within a method or lambda expression marked with the async modifier
We haven’t been taught lambda expression yet
Yep, that's true
Nothing to do with lambdas
All to do with async
await
can only be used inside of an async
method
If you haven't covered async/await yet, just use the synchronous version of this method
“Var can only appear within a declaration of script code”
“File is an ambiguous reference between …”
I’m getting to many errors
Is there a simple guide to how to manipulate files?
It shouldn’t be this hard or complicated in c#
Those are some weird errors
And you're right, it shouldn't be complicated
is a complete and ready C# code that will read the file and show it's contents when ran with
dotnet run
Post your code, let's see what's wrongSystem.IO
Is the namespace which contains all the base classes for reading and writing to/from files; right?
ye
I can’t post my code since I’m at uni
That means you are writing the code outside of a method, so its not the code itself, its where you typed it
The class I’m working in isn’t the main
Ugh guess I’ll just wait until I get home ig
Chances are, the issue is what Pobiega said
You're trying to use this as a default value of a field or some such
In that case, you should specify the type explicitly instead of using var
And tjat would be string?
Console.writeline does not exist in the current context
Seems like you are getting ahead of yourself, and being unable to share code means we cant really help
if you put those two lines of code in
Main
, it would just work
its also fine to put them in another method, and call that method from Main
Mhm
I’ll return later ig