❔ 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
K
Kouhai /人◕ ‿‿ ◕人\•383d ago
What problem are you facing?
AN
A Name•383d ago
nothing happens
K
Kouhai /人◕ ‿‿ ◕人\•383d ago
No UserData.txt file is created?
AN
A Name•383d ago
well the file is there
its just blank
K
Kouhai /人◕ ‿‿ ◕人\•383d ago
Try disposing the stream before exiting
UserSaveData.Dispose()
AN
A Name•383d ago
alright
just anywhere?
K
Kouhai /人◕ ‿‿ ◕人\•383d ago
Can you show the full code 😅 ?
AN
A Name•383d ago
accidiently closed it before it wrote ):
K
Kouhai /人◕ ‿‿ ◕人\•383d ago
Yeah, you only dispose the stream after you've done using it
AN
A Name•383d ago
gonna put it right after it writes
K
Kouhai /人◕ ‿‿ ◕人\•383d ago
No
AN
A Name•383d ago
oh
K
Kouhai /人◕ ‿‿ ◕人\•383d ago
Put it after you've written all your data
AN
A Name•383d 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?
K
Kouhai /人◕ ‿‿ ◕人\•383d ago
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.AN
A Name•383d ago
i see
so should i put this instead of the writeline i have now?
K
Kouhai /人◕ ‿‿ ◕人\•383d 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
AN
A Name•383d ago
for now its only used once
K
Kouhai /人◕ ‿‿ ◕人\•383d ago
Ah okay, then
using
expression won't be idealAN
A Name•383d ago
what might i use instead?
i cant really just call it when it closes
because crashes and stuff
K
Kouhai /人◕ ‿‿ ◕人\•383d ago
Just dispose the stream once you've done writing to it
A
Angius•383d ago
Just use
using
AN
A Name•383d ago
im still a little confused
A
Angius•383d ago
So it gets disposed when needed
AN
A Name•383d ago
do i need to redeclare it if i need it again?
with using
or a varriable
A
Angius•383d ago
K
Kouhai /人◕ ‿‿ ◕人\•383d ago
using
will dispose it automaticallyA
Angius•383d ago
It'll get disposed at the end of a scope
AN
A Name•383d ago
i see
if im using async tasks
nvm
K
Kouhai /人◕ ‿‿ ◕人\•383d ago
This won't work with your static field stream
AN
A Name•383d ago
thats a stupid question
to be clear do I need the full path everytime?
or can i just do it how you did
A
Angius•383d ago
You need a path
Mine was relative
AN
A Name•383d ago
ah
A
Angius•383d ago
You can use an absolute one
Or use the special directory enum thing
AN
A Name•383d ago
strings work as pathing correct
?
A
Angius•383d ago
Uh, yes
AN
A Name•383d ago
alr thanks
sorry to bother you again but i switched to using and it does not appear to work
A
Angius•383d ago
Show $code
M
MODiX•383d 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/A
Angius•383d ago
And elaborate on "does not appear to work"
AN
A Name•383d ago
its not doing anything
relevant lines:
outside of task
inside of task
A
Angius•383d ago
Ah, well, we're doing with asynchronous code, then?
AN
A Name•383d ago
yes
is that a problem?
A
Angius•383d ago
Might be
I'll need some more of this code
AN
A Name•383d ago
it uses dsharp is that ok?
thats a discord bot libary
A
Angius•383d ago
As long as you're not using
async void
methods, it should be fine
And as long as any async Task
methods are await
edAN
A Name•383d ago
A
Angius•383d ago
Ah, the streamwriter is instantiated inside of the main
And you're writing to it inside of an event
AN
A Name•383d ago
ah i see
i just need to move it inside?
works now thanks
A
Angius•383d ago
Tbh since you're not writing a lot, I'd just use
File.AppendAllTextAsync()
instead, for example
But if it works, it worksAN
A Name•383d 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?
A
Angius•383d ago
Odd requirement, but maybe?
Could do
Not sure about streamwriter tho
AN
A Name•383d 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
A
Angius•383d ago
Ideally, you'd implement some locking mechanism
Or give each user their own file
(at which point just use a database)
AN
A Name•383d 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
J
jcotton42•383d ago
@A Name what kind of application is this?
the approach you just described would be both difficult and error prone
AN
A Name•383d ago
a discord bot
J
jcotton42•383d ago
and what are you storing?
AN
A Name•383d ago
user data, mainly ints
J
jcotton42•383d ago
you really are going to want a database
sqlite, via ef core, would be a good option
P
Pixel•382d ago
Overview - Microsoft.Data.Sqlite
An overview of Microsoft.Data.Sqlite
P
Pixel•382d ago
This would be the library i'd recommend, quite nice to work with and has plenty of documentation
J
jcotton42•382d ago
That with EF on top yeah
A
Accord•374d 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.We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
51KMembers
View on DiscordWant results from more Discord servers?
More Posts❔ road mapi have a question, basic c# and .which topic should I study after netcore 5 training?✅ Testing GitHub webhooks on my local machineI'm currently developing a project using <https://github.com/octokit/webhooks.net>. What's the best ❔ How to prevent code from being leaked?When you created a tool and added a licence system like auth.gg, how can you make the code unreadabl❔ Http postHello guys My issue is when I click submit I get
This page isn’t working right now
If the problem co✅ How to get the form style of a processI need to get the form style of a process❔ Can i develop a discord music bot or admin bot using C#?If i can, how? Any idea?❔ Console inputHi, is it possible to enable console input in web sdk using Host?
```csharp
public static as❔ MediaPlayer in WPF occasionally fails```cs
public static void PlaySound(string path)
{
Task.Run(() =>
❔ How to Create a C# Code Base for a PC CleanerDoes anyone know of C# to make a base code that for now just clean the %temp% folder just for me to ✅ How to install a callback for an awaited item when using Task.WhenAllHello I am using Task.WhenAll on a List of Tasks. However if a task fails I want to immediately call❔ How to get azure cache for Redis connection string stored in app service as a appsetting variableI have created a azure redis for cache and stored the connection string as application setting varia❔ Giveaway for an online DOTNET ConferenceHello developer students,
I am going to give away free tickets to an online dotnet conference whic❔ SoundBoardHi!
I am working on a soundboard lately and i got stuck with playing a sound throught microphone.
i'❔ accessing object from a class inside of another classI got this class called "Engine"(image 1) that has the object "isOn" which indicates whether the eng❔ QuestionControllerHello, i have a question regarding API structure.
I have two classses, Item and Store. I created C❔ Problem Binding to Image SizeI am making an app that displays a series of books. An image and title of the book is to be displaye❔ whats the best site or youtube channel to start learning c#?just downloaded visual studio and wanted to know what would be the best way to learn C#❔ C# MVC - Erroring in VSC not VS2022I'm writing a C# .Net Core 7.0 web application with dapper for my SQL queries.
It runs just fine inString Contain issueSo im trying to get a word in a string using Contain but its not getting the word, I havent found a ❔ Catching Exceptions with generic parameters?When adding an entity to the database, i first check if it already exists and if it does I throw an