✅ 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)
No description
160 Replies
Waffles from Human Resources
heres a different project ive done the exact same thing in and it worked perfectly fine
No description
reflectronic
reflectronic4mo ago
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.
rajesh1969
rajesh19694mo ago
You could check whether the application has rights to write the file in that folder
Waffles from Human Resources
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
rajesh1969
rajesh19694mo ago
Remove using and try once whether it works Or 2. Use File.AppendAllText Without using stream
Waffles from Human Resources
I did that too
var f = File.CreateText("config.json");
f.Write(config);
f.Close();
var f = File.CreateText("config.json");
f.Write(config);
f.Close();
rajesh1969
rajesh19694mo ago
Just write File.AppendAllText(“stickerdb\config.json”,config)
Unknown User
Unknown User4mo ago
Message Not Public
Sign In & Join Server To View
Waffles from Human Resources
Afaik using handles all the closing and stuff That's why I use it mostly
Unknown User
Unknown User4mo ago
Message Not Public
Sign In & Join Server To View
Unknown User
Unknown User4mo ago
Message Not Public
Sign In & Join Server To View
Waffles from Human Resources
I will once im at my pc
Unknown User
Unknown User4mo ago
Message Not Public
Sign In & Join Server To View
Waffles from Human Resources
What do you mean? Ah Sorry didn't see the proper mag Msg*
Unknown User
Unknown User4mo ago
Message Not Public
Sign In & Join Server To View
Waffles from Human Resources
I mostly use JObjects for small config
Unknown User
Unknown User4mo ago
Message Not Public
Sign In & Join Server To View
Waffles from Human Resources
Like this config currently only has like 1 key
Unknown User
Unknown User4mo ago
Message Not Public
Sign In & Join Server To View
Waffles from Human Resources
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
Unknown User4mo ago
Message Not Public
Sign In & Join Server To View
Waffles from Human Resources
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
MODiX
MODiX4mo ago
TeBeCo
public class Foo
{
public string Bar {get;set;}
}
public class Foo
{
public string Bar {get;set;}
}
await JsonSerializer.WriteJsonAsync(config, stream)
await JsonSerializer.WriteJsonAsync(config, stream)
Quoted by
React with ❌ to remove this embed.
Unknown User
Unknown User4mo ago
Message Not Public
Sign In & Join Server To View
Waffles from Human Resources
no it was more like wait
Unknown User
Unknown User4mo ago
Message Not Public
Sign In & Join Server To View
Waffles from Human Resources
something like this
public class MyClass {

[JsonPropertiyName("someJsonKey")]
public string testString {get;set;}

}
public class MyClass {

[JsonPropertiyName("someJsonKey")]
public string testString {get;set;}

}
and then theyd do smth like
JsonSerializer.Deserialize<MyClass>(json);
JsonSerializer.Deserialize<MyClass>(json);
or smth
Unknown User
Unknown User4mo ago
Message Not Public
Sign In & Join Server To View
Waffles from Human Resources
also @/reflectronic was right, the config appears to be null
Unknown User
Unknown User4mo ago
Message Not Public
Sign In & Join Server To View
Waffles from Human Resources
the thing is that i work a lot with REST API stuff so i constantly parse JSON back and forth
Unknown User
Unknown User4mo ago
Message Not Public
Sign In & Join Server To View
Unknown User
Unknown User4mo ago
Message Not Public
Sign In & Join Server To View
Waffles from Human Resources
thats cool
Unknown User
Unknown User4mo ago
Message Not Public
Sign In & Join Server To View
Unknown User
Unknown User4mo ago
Message Not Public
Sign In & Join Server To View
Waffles from Human Resources
the project itself is an ASP.NET Core project
Unknown User
Unknown User4mo ago
Message Not Public
Sign In & Join Server To View
Waffles from Human Resources
simple config for storing some values i need later on
Unknown User
Unknown User4mo ago
Message Not Public
Sign In & Join Server To View
Waffles from Human Resources
probably never but i always make sure i have the chance to change it if its needed
Unknown User
Unknown User4mo ago
Message Not Public
Sign In & Join Server To View
Waffles from Human Resources
uh i just re run
Unknown User
Unknown User4mo ago
Message Not Public
Sign In & Join Server To View
Waffles from Human Resources
yes the content of it for this project is pretty static so
Unknown User
Unknown User4mo ago
Message Not Public
Sign In & Join Server To View
Unknown User
Unknown User4mo ago
Message Not Public
Sign In & Join Server To View
Waffles from Human Resources
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
leowest
leowest4mo ago
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*
Waffles from Human Resources
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
leowest
leowest4mo ago
and what is the path?
Waffles from Human Resources
is this some ASP.NET thing StickerDB/config.json
leowest
leowest4mo ago
the full path obviously
Waffles from Human Resources
C:\Users\Fabio\source\repos\StickerDBServer\StickerDBServer\bin\Debug\net8.0\StickerDB\config.json
leowest
leowest4mo ago
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
Waffles from Human Resources
the same issue happened yesterday but i blamed StreamWriter not writing
leowest
leowest4mo ago
and then u said your object was null
Waffles from Human Resources
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
leowest
leowest4mo ago
can you provide your code where you deserialize and where u serialize your file
Waffles from Human Resources
this config will return null cause it cant read it yesterday it returned null because there was no valid JSON
No description
leowest
leowest4mo ago
Why are you using JObject that is just bad
Waffles from Human Resources
used to it
leowest
leowest4mo ago
Hopefully they told u that already
leowest
leowest4mo ago
well u should check if the file exists before reading it
leowest
leowest4mo ago
there we go
Waffles from Human Resources
CreateFiles never seems to get called
leowest
leowest4mo ago
U dont need to check for directory and file
Waffles from Human Resources
my main issue is the permissions rn
leowest
leowest4mo ago
File.Exists will fail if the directrory does not exist either
Waffles from Human Resources
and why id need admin for this project when 10 others worked
leowest
leowest4mo ago
so you can just do
if (File.Exists("StickerDB/config.json"))
{
ReadFiles();
}
else
{
CreateFiles();
}
if (File.Exists("StickerDB/config.json"))
{
ReadFiles();
}
else
{
CreateFiles();
}
also why do u create the files in both the if and else? shouldn't one be reading it and the other creating
Waffles from Human Resources
im checking if it doesnt exist
leowest
leowest4mo ago
can you show the code for CreateFiles
leowest
leowest4mo ago
like the whole method if possible
Waffles from Human Resources
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
leowest
leowest4mo ago
so reality is u never use
Waffles from Human Resources
and i dont understand why this isnt abt the files anymore they work its abt windows thinking i need admin
leowest
leowest4mo ago
its an asp.net project?
Waffles from Human Resources
yes id blame the project type for it but ive done this before on ASP.NET aswell
leowest
leowest4mo ago
can u check if u set the folders properties to readonly where ur project exists or any file it needs to read write
leowest
leowest4mo ago
No description
Waffles from Human Resources
everything has write perms i think
leowest
leowest4mo ago
well double check
Waffles from Human Resources
it just re enables itself i turn off read only and it turns on
leowest
leowest4mo ago
well that is not a normal behaviors
Waffles from Human Resources
it seems like it doesnt even apply it i uncheck it and confirm then when i check again its on
leowest
leowest4mo ago
and you're logged in as admin
Waffles from Human Resources
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
leowest
leowest4mo ago
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
Waffles from Human Resources
nono it works but in the wrong path
leowest
leowest4mo ago
👆 we are talking about the right path not somewhere else it randomly create a folder for whatever reason
Waffles from Human Resources
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?
leowest
leowest4mo ago
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.json
try 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
Waffles from Human Resources
this is not supposed to be here at all
No description
leowest
leowest4mo ago
can you show me a screenshot of your Solution Project as well
leowest
leowest4mo ago
sorry Solution Explorer
leowest
leowest4mo ago
the window that list all files
Waffles from Human Resources
Utils holds the file creation
leowest
leowest4mo ago
so if u collapse StickerDB
Waffles from Human Resources
StickerDB folder isnt supposed to b there
leowest
leowest4mo ago
there is a config.json there?
Waffles from Human Resources
everything as expected just in the wrong path
leowest
leowest4mo ago
ok if u select config.json and look at the window properties does it says cofy if newer or copy always
Waffles from Human Resources
copy if newer
leowest
leowest4mo ago
ok so you understand that, the reason u have
C:\Users\Fabio\source\repos\StickerDBServer\StickerDBServer\StickerDB
is 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
Waffles from Human Resources
i never had issues with this before
leowest
leowest4mo ago
this is not an issue its literally how it works
Waffles from Human Resources
then why isnt it consistent
leowest
leowest4mo ago
if you set something to copy always or copy if newer
Waffles from Human Resources
but i didnt set anything
leowest
leowest4mo ago
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
Waffles from Human Resources
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
leowest
leowest4mo ago
if they were not supposed to be there then delete it
Waffles from Human Resources
they will just re create
leowest
leowest4mo ago
no they wont
Waffles from Human Resources
they do i keep deleting them
leowest
leowest4mo ago
for them to show up in there they also need to be set inside the csproj file its not just recreate
Waffles from Human Resources
this is not where its supposed to be
No description
leowest
leowest4mo ago
ok then delete it from your Solution Explorer
Waffles from Human Resources
i did. and then i re ran and it was back
leowest
leowest4mo ago
is your project on github?
leowest
leowest4mo ago
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
Waffles from Human Resources
all the code ive shown you is all the code there is
leowest
leowest4mo ago
the few code u have shown should not make that happen
Waffles from Human Resources
this is all the code of the entire project if we dont consider the database manager
leowest
leowest4mo ago
ok, and were u able to do this
Waffles from Human Resources
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
leowest
leowest4mo ago
well that is a problem, read only means you cannot modify anything
Waffles from Human Resources
and the folder its in and so on
leowest
leowest4mo ago
can you create a new project and copy over just the code and see if that happens
Waffles from Human Resources
wait i will try to rename the folder to _config at the end maybe that works
leowest
leowest4mo ago
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
Waffles from Human Resources
i did same result
leowest
leowest4mo ago
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
Waffles from Human Resources
for testing should i make another ASP.NET project or a console app
leowest
leowest4mo ago
asp.net exact the same if possible which asp.net project u had selected?
Waffles from Human Resources
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
leowest
leowest4mo ago
but u create a new solution right u didnt just add it to the same solution as StickerDB?
Waffles from Human Resources
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??
leowest
leowest4mo ago
for asp.net?
Waffles from Human Resources
yes the issue is ASP.NET
leowest
leowest4mo ago
shouldn't but in your case that is what is happening
Waffles from Human Resources
ill just go with it