C
C#2y ago
A Name

❔ 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:
public static StreamWriter UserSaveData = new("C:\\Users\\name\\source\\repos\\AuctionHouseDiscord\\AuctionHouseDiscord\\UserData.txt");
public static StreamWriter UserSaveData = new("C:\\Users\\name\\source\\repos\\AuctionHouseDiscord\\AuctionHouseDiscord\\UserData.txt");
other code
//galleon count is an int
//withing an async task if that matters
UserSaveData.WriteLine(GalleonCount.ToString());
//galleon count is an int
//withing an async task if that matters
UserSaveData.WriteLine(GalleonCount.ToString());
64 Replies
Kouhai
Kouhai2y ago
What problem are you facing?
A Name
A NameOP2y ago
nothing happens
Kouhai
Kouhai2y ago
No UserData.txt file is created?
A Name
A NameOP2y ago
well the file is there its just blank
Kouhai
Kouhai2y ago
Try disposing the stream before exiting UserSaveData.Dispose()
A Name
A NameOP2y ago
alright just anywhere?
Kouhai
Kouhai2y ago
Can you show the full code 😅 ?
A Name
A NameOP2y ago
accidiently closed it before it wrote ):
Kouhai
Kouhai2y ago
Yeah, you only dispose the stream after you've done using it
A Name
A NameOP2y ago
gonna put it right after it writes
Kouhai
Kouhai2y ago
No
A Name
A NameOP2y ago
oh
Kouhai
Kouhai2y ago
Put it after you've written all your data
A Name
A NameOP2y ago
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?
Kouhai
Kouhai2y ago
Typically you call dispose with using expression like this This will automatically call dispose when the scope ends
using var UserSaveData = new StreamWriter("C:\\Users\\name\\source\\repos\\AuctionHouseDiscord\\AuctionHouseDiscord\\UserData.txt");
UserSaveData.WriteLine(GalleonCount.ToString());
using var UserSaveData = new StreamWriter("C:\\Users\\name\\source\\repos\\AuctionHouseDiscord\\AuctionHouseDiscord\\UserData.txt");
UserSaveData.WriteLine(GalleonCount.ToString());
The implementation of dispose depends on the type, for streamwriter it flushes the data and closes the stream.
A Name
A NameOP2y ago
i see so should i put this instead of the writeline i have now?
Kouhai
Kouhai2y ago
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 ThumbsUpSmile
A Name
A NameOP2y ago
for now its only used once
Kouhai
Kouhai2y ago
Ah okay, then using expression won't be ideal
A Name
A NameOP2y ago
what might i use instead? i cant really just call it when it closes because crashes and stuff
Kouhai
Kouhai2y ago
Just dispose the stream once you've done writing to it
Angius
Angius2y ago
Just use using
A Name
A NameOP2y ago
im still a little confused
Angius
Angius2y ago
So it gets disposed when needed
A Name
A NameOP2y ago
do i need to redeclare it if i need it again? with using or a varriable
Angius
Angius2y ago
using var sw = new StreamWriter("file.txt");
sw.WriteLine("Hello World!");
using var sw = new StreamWriter("file.txt");
sw.WriteLine("Hello World!");
Kouhai
Kouhai2y ago
using will dispose it automatically
Angius
Angius2y ago
It'll get disposed at the end of a scope
A Name
A NameOP2y ago
i see if im using async tasks nvm
Kouhai
Kouhai2y ago
This won't work with your static field stream
A Name
A NameOP2y ago
thats a stupid question to be clear do I need the full path everytime? or can i just do it how you did
Angius
Angius2y ago
You need a path Mine was relative
A Name
A NameOP2y ago
ah
Angius
Angius2y ago
You can use an absolute one Or use the special directory enum thing
A Name
A NameOP2y ago
strings work as pathing correct ?
Angius
Angius2y ago
Uh, yes
A Name
A NameOP2y ago
alr thanks sorry to bother you again but i switched to using and it does not appear to work
Angius
Angius2y ago
Show $code
MODiX
MODiX2y ago
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/
Angius
Angius2y ago
And elaborate on "does not appear to work"
A Name
A NameOP2y ago
its not doing anything relevant lines: outside of task
public static string UserDataPath = "C:\\Users\\Jason Lin\\source\\repos\\AuctionHouseDiscord\\AuctionHouseDiscord\\UserData.txt";
public static string UserDataPath = "C:\\Users\\Jason Lin\\source\\repos\\AuctionHouseDiscord\\AuctionHouseDiscord\\UserData.txt";
inside of task
using var UserData = new StreamWriter(UserDataPath);
using var UserData = new StreamWriter(UserDataPath);
UserData.WriteLine(GalleonCount.ToString());
UserData.WriteLine(GalleonCount.ToString());
Angius
Angius2y ago
Ah, well, we're doing with asynchronous code, then?
A Name
A NameOP2y ago
yes is that a problem?
Angius
Angius2y ago
Might be I'll need some more of this code
A Name
A NameOP2y ago
it uses dsharp is that ok? thats a discord bot libary
Angius
Angius2y ago
As long as you're not using async void methods, it should be fine And as long as any async Task methods are awaited
Angius
Angius2y ago
Ah, the streamwriter is instantiated inside of the main And you're writing to it inside of an event
A Name
A NameOP2y ago
ah i see i just need to move it inside? works now thanks
Angius
Angius2y ago
Tbh since you're not writing a lot, I'd just use File.AppendAllTextAsync() instead, for example But if it works, it works
A Name
A NameOP2y ago
planning 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?
Angius
Angius2y ago
Odd requirement, but maybe? Could do
var lines = File.ReadAllLines(fileName);
lines[line_to_edit] = newText;
File.WriteAllLines(fileName, lines);
var lines = File.ReadAllLines(fileName);
lines[line_to_edit] = newText;
File.WriteAllLines(fileName, lines);
Not sure about streamwriter tho
A Name
A NameOP2y ago
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
Angius
Angius2y ago
Ideally, you'd implement some locking mechanism Or give each user their own file (at which point just use a database)
A Name
A NameOP2y ago
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
jcotton42
jcotton422y ago
@A Name what kind of application is this? the approach you just described would be both difficult and error prone
A Name
A NameOP2y ago
a discord bot
jcotton42
jcotton422y ago
and what are you storing?
A Name
A NameOP2y ago
user data, mainly ints
jcotton42
jcotton422y ago
you really are going to want a database sqlite, via ef core, would be a good option
Pixel
Pixel2y ago
This would be the library i'd recommend, quite nice to work with and has plenty of documentation
jcotton42
jcotton422y ago
That with EF on top yeah
Accord
Accord2y ago
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.
Want results from more Discord servers?
Add your server