✅ StreamWriter not writing to file
Im trying to create a config file based on a JObject from Newtonsoft.Json, but it doesnt seem to write to my file despite creating it
I have no idea why this error occurres as i have done this many times in the past with no issue (In case anyone wants me to test, i put all sorts of stuff into the .Write functions and none of them worked)
160 Replies
heres a different project ive done the exact same thing in and it worked perfectly fine
have you used a debugger to see what is happening
are you sure this code is running, are you sure
config
is not empty, etc.You could check whether the application has rights to write the file in that folder
The program checks if the file exists -> if not it will declare a JObject for the config -> create the folder -> create an empty config.json inside of the folder
Everything works except the text actually being written
I'm running in the same path as the program
Also yes, the program even creates the config file it just doesn't write to it
Remove using and try once whether it works
Or
2. Use File.AppendAllText
Without using stream
I did that too
Just write
File.AppendAllText(“stickerdb\config.json”,config)
Oki
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
Afaik using handles all the closing and stuff
That's why I use it mostly
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
Ah
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
I will once im at my pc
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
What do you mean?
Ah
Sorry didn't see the proper mag
Msg*
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
I mostly use JObjects for small config
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
Like this config currently only has like 1 key
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
But yes I plan to get into that class stuff later on this yr cause I been deserializing json in a shitty way
I've seen some interesting stuff
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
ive seen some way that allows ppl to create a class and then assign attributes to the values which then allowed them to deserialize the JSON as that class or smth
seems super convinient
TeBeCo
Quoted by
<@689473681302224947> from #StreamWriter not writing to file (click here)
React with ❌ to remove this embed.
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
no it was more like
wait
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
something like this
and then theyd do smth like
or smth
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
also @/reflectronic was right, the config appears to be
null
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
the thing is that i work a lot with REST API stuff so i constantly parse JSON back and forth
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
ah
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
thats cool
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
hm
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
the project itself is an ASP.NET Core project
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
simple config for storing some values i need later on
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
probably never but i always make sure i have the chance to change it if its needed
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
uh
i just re run
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
yes
the content of it for this project is pretty static so
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
wym
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
i found the issue and i dont understand it
the program needs admin rights to run in the working directory to read and write files?????
this has never happened before
if your application is inside a system folder or Program files or Program Files x86 then it will need permission to do stuff and you're trying to write the file to those folders*
its not
its in the same path where all my projects are
and on all projects i do the same config
wtf is going on with this project
and what is the path?
is this some ASP.NET thing
StickerDB/config.json
the full path
obviously
C:\Users\Fabio\source\repos\StickerDBServer\StickerDBServer\bin\Debug\net8.0\StickerDB\config.json
yeah doesn't look like a critical path
at this point I would try to restart windows since it looks like you're getting a bunch fo weird stuff going on
and then try again
the same issue happened yesterday but i blamed StreamWriter not writing
and then u said your object was null
i mean it didnt write anything but it also never checked if the stuff existed to the create function never got called
on reading
i thought its writing first
can you provide your code where you deserialize and where u serialize your file
this config will return
null
cause it cant read it
yesterday it returned null
because there was no valid JSONWhy are you using JObject that is just bad
used to it
Hopefully they told u that already
yes.
well u should check if the file exists before reading it
there we go
CreateFiles never seems to get called
U dont need to check for directory and file
my main issue is the permissions rn
File.Exists will fail if the directrory does not exist either
and why id need admin for this project when 10 others worked
so you can just do
also why do u create the files in both the if and else?
shouldn't one be reading it and the other creating
im checking if it doesnt exist
can you show the code for CreateFiles
here
like the whole method if possible
i also attached a screenshot of a different project which works perfectly fine
that is the whole method
except that there is a config being declared and stuff
when i run as admin all of this works as expected
so reality is u never use
and i dont understand why
this isnt abt the files anymore they work its abt windows thinking i need admin
its an asp.net project?
yes
id blame the project type for it but ive done this before
on ASP.NET aswell
can u check if u set the folders properties to readonly
where ur project exists
or any file it needs to read write
everything has write perms
i think
well double check
it just re enables itself
i turn off read only and it turns on
well that is not a normal behaviors
it seems like it doesnt even apply it
i uncheck it and confirm
then when i check again its on
and you're logged in as admin
there is also a StickerDB folder in where the .csproj file is
i have no idea whats going on
why does it create itself there now
it even works there????
thats not even where the executable is
well check if the previous folder is also read only until u get to a folder that is not read only
and then uncheck it in order
see if that works
nono it works
but in the wrong path
👆
we are talking about the right path not somewhere else it randomly create a folder for whatever reason
expected path:
C:\Users\Fabio\source\repos\StickerDBServer\StickerDBServer\bin\Debug\net8.0\StickerDB\config.json
actual path: C:\Users\Fabio\source\repos\StickerDBServer\StickerDBServer\StickerDB
is that some .NET 8 thing?no its not, if I had to guess at some point u tried something that caused it to put the folder there
either by mistake or w/e
C:\Users\Fabio\source\repos\StickerDBServer\StickerDBServer\bin\Debug\net8.0\StickerDB\config.jsontry to go up a level until u find a folder that is not read only and then go from there removing the read only status
this is not supposed to be here at all
oki
can you show me a screenshot of your Solution Project as well
this?
sorry Solution Explorer
ah
the window that list all files
Utils holds the file creation
so if u collapse StickerDB
StickerDB folder isnt supposed to b there
there is a config.json there?
everything as expected
just in the wrong path
ok if u select config.json
and look at the window properties
does it says cofy if newer or copy always
copy if newer
ok so you understand that, the reason u have
C:\Users\Fabio\source\repos\StickerDBServer\StickerDBServer\StickerDBis because you have it in your porject right and that copy if newer means it will create a copy of it to
C:\Users\Fabio\source\repos\StickerDBServer\StickerDBServer\bin\Debug\net8.0\StickerDB\config.json
i never had issues with this before
this is not an issue its literally how it works
then why isnt it consistent
if you set something to copy always or copy if newer
but i didnt set anything
it will copy it to the output folder then u run or build
wdym u dont set anything we literally just chedk in your Solution Explorer
and config,json is set to copy
if u dont want that to happen then u have to set it to none
for the .db file and for the .config file in your solution explorer in their properties
but why is it there in the first place
if i do smth like this it usually ends up where the executable is
but this is where the .csproj file is
if they were not supposed to be there then delete it
they will just re create
no they wont
they do
i keep deleting them
for them to show up in there
they also need to be set inside the csproj file
its not just recreate
this is not where its supposed to be
this is
ok then delete it from your Solution Explorer
i did.
and then i re ran
and it was back
is your project on github?
no
can you put it there so I can take a look?
because there is way too many weird stuff happening
and unless I can see the code its hard to tell why X or Y happens
all the code ive shown you is all the code there is
the few code u have shown should not make that happen
this is all the code of the entire project if we dont consider the database manager
ok, and were u able to do this
they are all read only
and it doesnt matter how often i turn it off it seems to just re check
even the config folder itself is read only
well that is a problem, read only means you cannot modify anything
and the folder its in
and so on
can you create a new project
and copy over just the code
and see if that happens
wait
i will try to rename the folder
to _config at the end
maybe that works
preferable not in the same solution
if the folder above it, is also read only then it wont
u could delete the bin folder
if StickerDB is not read only
it should recreate everything properly
i did
same result
well then I would suggest you to create a new solution with your project
and move the files and see if that one works
because that behavior AFAIK is not normal
for testing should i make another ASP.NET project or a console app
asp.net
exact the same
if possible
which asp.net project u had selected?
it did it again
ASP.NET Core Web-API i think
different project same result
atp its easier to write my own webserver with HttpListener
but u create a new solution right u didnt just add it to the same solution as StickerDB?
yes
different project
ASP.NET is the problem
Console app works as expected
where tf does the program run then
if i run the .exe in the build path it also works as expected
this means the programs runs in the wrong path??
for asp.net?
yes
the issue is ASP.NET
shouldn't but in your case that is what is happening
ill just go with it