❔ Writing to StreamWriter Fails
Hi, I was experimenting with permanently saving things for the first time and ran into problems. Despite the code looking right, it doesn't seem to work. I'm relatively new to this, so it's likely i messed something up, but help would be appericiated
Related code below:
other code
64 Replies
What problem are you facing?
nothing happens
No UserData.txt file is created?
well the file is there
its just blank
Try disposing the stream before exiting
UserSaveData.Dispose()
alright
just anywhere?
Can you show the full code 😅 ?
accidiently closed it before it wrote ):
Yeah, you only dispose the stream after you've done using it
gonna put it right after it writes
No
oh
Put it after you've written all your data
i only write once
oh
it just started working
thanks
whats dispose do exactly?
oh
does it dump everything youve written into the text file?
Typically you call dispose with
using
expression like this
This will automatically call dispose when the scope ends
The implementation of dispose depends on the type, for streamwriter it flushes the data and closes the stream.i see
so should i put this instead of the writeline i have now?
Well, I don't know why you have UserSaveData as a static field
If it's only used once, then declare it and initialize it in the scope that uses it
for now its only used once
Ah okay, then
using
expression won't be idealwhat might i use instead?
i cant really just call it when it closes
because crashes and stuff
Just dispose the stream once you've done writing to it
Just use
using
im still a little confused
So it gets disposed when needed
do i need to redeclare it if i need it again?
with using
or a varriable
using
will dispose it automaticallyIt'll get disposed at the end of a scope
i see
if im using async tasks
nvm
This won't work with your static field stream
thats a stupid question
to be clear do I need the full path everytime?
or can i just do it how you did
You need a path
Mine was relative
ah
You can use an absolute one
Or use the special directory enum thing
strings work as pathing correct
?
Uh, yes
alr thanks
sorry to bother you again but i switched to using and it does not appear to work
Show $code
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
If your code is too long, post it to: https://paste.mod.gg/And elaborate on "does not appear to work"
its not doing anything
relevant lines:
outside of task
inside of task
Ah, well, we're doing with asynchronous code, then?
yes
is that a problem?
Might be
I'll need some more of this code
it uses dsharp is that ok?
thats a discord bot libary
As long as you're not using
async void
methods, it should be fine
And as long as any async Task
methods are await
edAh, the streamwriter is instantiated inside of the main
And you're writing to it inside of an event
ah i see
i just need to move it inside?
works now thanks
Tbh since you're not writing a lot, I'd just use
File.AppendAllTextAsync()
instead, for example
But if it works, it worksplanning to write more later
not a completely related question, but is there a way to write on a specfic line thats not the last
like for example if i want to write to line 5 specfically?
Odd requirement, but maybe?
Could do
Not sure about streamwriter tho
doesnt really work due to what i orignally intended but thanks anyways
wanted to avoid data being lost when two people changed things at same time
Ideally, you'd implement some locking mechanism
Or give each user their own file
(at which point just use a database)
im thinking of making it just update every one second
like it saves all the data in a list
then writes all the data after one second
theoretically that should avoid data loss
theoretically
but if someone writes data in that one second it might get duplicated ahhhh
@A Name what kind of application is this?
the approach you just described would be both difficult and error prone
a discord bot
and what are you storing?
user data, mainly ints
you really are going to want a database
sqlite, via ef core, would be a good option
Overview - Microsoft.Data.Sqlite
An overview of Microsoft.Data.Sqlite
This would be the library i'd recommend, quite nice to work with and has plenty of documentation
That with EF on top yeah
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.